wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes methods throwing exceptions!!!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "methods throwing exceptions!!!!!!" Watch "methods throwing exceptions!!!!!!" New topic
Author

methods throwing exceptions!!!!!!

Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Considering this statement: if a statement within a method throws an exception, that exception is thrown to the calling method.
this indicates that exception are thrown automatically to the caller if they are not handled. if this is the case then what do we benefit from declaring that a method that throws an exception? for example:


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Vicken
By doing this you inform the caller that this method may throw the exception stated in the declaration and the caller may take suitable measures to catch the exception.
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
if this is the case then ommiting it won't do any harm either, since i known (as a programmer) that it will throw an exception, so i can take the appropriate action to catch it, right?
it doesn't make sence...
[ July 23, 2003: Message edited by: Vicken Karaoghlanian ]
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Originally posted by Vicken Karaoghlanian:
if this is the case then ommiting it won't do any harm either, sice i known (as a programmer) that it will throw an exception, so i can take the appropriate action to catch it, right?

Yup, either you declare that the method might throw the exception, or catch it with a try/catch block.


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Vicken
Well it does makes sense. What if you are using classes developed by someone else. That is you don't have the source code for the class.
Nad Shez
Greenhorn

Joined: Jun 17, 2003
Posts: 9
I think a more appropriate statement would be
if a statement within a method throws a checked exception, that exception must be dealt within that method using a try/catch block or declared in the throws clause of the method
consider this simple code

aMethod() throws an exception at line 9, so it can either deal with this exception in the method body by using a try/catch block (which it doesn't!) OR propagate it to the caller by declaring it in its throws clause (which it does at line 8).
Next in the line is method main() (that btw too can declare Exceptions in its throws clause, just like any other method) that called aMethod() at line 4. Again, it can either deal with this probability of getting an exception by either using a try/catch block (which it does) OR propagate it to the caller (System in main's case) by declaring it in its throws clause.
Since aMethod() declares that it might throw an Exception, any method calling aMethod() must use a try/catch block or propagate it using throws clause. Failing to do either flags a compiler error. If aMethod() hadn't declared, then there is no responsibility on the calling body, but then aMethod() itself must deal with any exceptions it throws using a try/catch block.
so remember that you can't get past the compiler unless you "deal" with a checked exception by either using a try/catch block or propagating it using the throws clause.
Note that the rule DOES NOT apply to unchecked exception (Error, RuntimeException, & all subclasses of these two) are known as unchecked exception. All the rest are checked exceptions. A good program is not supposed to throw any unchecked exceptions in the first place, so a compiler gives no error if you throw a unchecked exception & don't "deal" with it in the above described manner.
I suggest you experiment with the above code. e.g. remove the "throws Exception" from aMethod(), compile & see what happens .. or comment try/catch block around aMethod(), compile & see what happens. Also try replacing Exception() at line 9 with RuntimeException() or Error() to get a better understanding of unchecked exception.
Hope this helps.
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
thx all for ur replies.
i have one more question left. Nad, you've mentioned that the main method throws exceptions like any other method (that is true), but the question that has been bothering me is for whome?
the main is the root of all methods, it doesn't make sence that it should throw exceptions.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Vicken
Generally it doesn't makes much sense to have the throws clause with the main(String args[]) method. But at some places it might be advisable like for example when you are calling the main() method from within your application.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
If your main method throws an exception, your program will die, and you'll see a stack trace.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Ron
Originally posted by Ron Newman:
If your main method throws an exception, your program will die, and you'll see a stack trace.

In the following code the main method throws an exception and that is caught. No stack trace is printed.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Well, that code is a bit perverse. Normally, you don't call main() from elsewhere.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Ron
Originally posted by Ron Newman:
Well, that code is a bit perverse. Normally, you don't call main() from elsewhere.

That is what I said in my last to last post in this thread.
Originally posted by Anupam Sinha:
Hi Vicken
Generally it doesn't makes much sense to have the throws clause with the main(String args[]) method. But at some places it might be advisable like for example when you are calling the main() method from within your application.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: methods throwing exceptions!!!!!!
 
Similar Threads
Overriding methods & Exceptions
exceptions:catch or specify requirement
Understanding Exceptions
throws and throw in exception
The "throw" where statement?