• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception - throw

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic