Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception - throw

 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic