*
The moose likes Beginning Java and the fly likes Exception - throw Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception - throw" Watch "Exception - throw" New topic
Author

Exception - throw

Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
This is a piece of code form Sun's website



What I am wondering is that why do we need "throw". Since we are using "throws", then if an error occurs in any part of the code within the method, it will obviously be throwing that error.


Regards,<br />Arnab
Vijay Raj
Ranch Hand

Joined: Oct 10, 2005
Posts: 110
EmptyStackException is throws because the stack is empty. How do we find whether the stack is empty or not? By looking at the size and therefore


Now what. We need to throw the exception. How do we do it? By saying


I want the programmer using this function to catch it. How do I do it. By saying



If the throw new EmptyStackException() is removed from the code you posted, will the EmptyStackException be ever thrown. We have to make the function throw the exception intentionally.

regards,
vijay.
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Your doubt stems from the fact that you think only the existing code can generate an exception. That is not true. Even you can generate exceptions, and you do that by using the THROW keyword.


ASCII silly question, Get a silly ANSI.
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
so continuing with that reasoning...

If I specifically do not want to throw an error, if I have "throws" associated with the method, then if during the execution of the program, that particular method ever results in an exception, it will automatically be thrown.

Only if I purposely want some part of that method to throw some exception, I will be using "throw".

Correct ?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think you got it. "Throws" warns people who call a method that it MIGHT throw an exception. That can happen if you execute "throw" or if some method you call executes a "throw" and you do nothing about it.

The following will not compile, because FileInputStream.read() says it might throw IOException and we neither handle it or declar that we might throw it.

We can do two different things about this:

That's all dealing with methods that we call that might throw exceptions. You can also throw them on your own as your original example showed. Your example doesn't ALWAYS throw one, but it could if it wanted to because it didn't like the current state of some variable or parameter. FileInputStream is the same way. It doesn't always throw exceptions; only if you try something "exceptional" like reading a file that isn't there.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
It seems like there's still an important misunderstanding here:

[Arnb]: Since we are using "throws", then if an error occurs in any part of the code within the method, it will obviously be throwing that error.

[...]

[Arnb]: If I specifically do not want to throw an error, if I have "throws" associated with the method, then if during the execution of the program, that particular method ever results in an exception, it will automatically be thrown.


No. Just because throws lists a particular exception for a method, that does not cause the JVM to only throw that particular exception from the method. It's also possible to throw a subclass of the declared exception(s), or to throw any RuntimeException, or any Error. Including any subclass of RuntimeException or Error - these are collectively known as unchecked exceptions.

It sounds like you're imagining that the throws clause does more than it really does. It does not tell the JVM what kind of exception it must throw. It only provides a warning about some types of exceptions that might be thrown. And the list is generally incomplete, as there are usually many unchecked exceptions which are theoretically possible but aren't listed in the throws clause.

So how does the JVM know what type of exception to actually throw? That depends on the exact problem that occcurred, and how the code is written. If you write code that creates a specific exception and throws it (with "throw", not "throws") then that's the exception that gets throw. Alternately if you violate certain rules of Java you will get a specific excption thrown. E.g. this code:

will always throw a NullPointerException. Even though the containing method probably doesn't say "throws NullP{ointerException", and no one wrote "throw new NulllPointerException()" anywhere in the method. There are many other exceptions and errors which may arise from specific situations: ArrayIndexOutOfBounds, ClassNotFoundException, OutOfMemoryError, etc. What gets thrown depends on what specific rule may have been violated.
[ November 28, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
Thanks for all that explanation. I will be trying few more examples to see throw and throws in action.

Any pointers to explanatory sample codes - not just theory ?
 
wood burning stoves
 
subject: Exception - throw