Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes throwing Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "throwing Exceptions" Watch "throwing Exceptions" New topic
Author

throwing Exceptions

Jerry Bustamente
Ranch Hand

Joined: May 24, 2004
Posts: 90
I need some help understanding an example I found in Sierra Bates. On page 252 they list an exam watch warning to make sure handle and declare are handled properly.

The code is as follows:

1. class MyException extends Exception {
2. void someMethod () {
3. do stuff();
4. }
5. void doStuff() throws MyException {
6. try {
7. throw new MyException();
8. }
9. catch (MyException me) {
10. throw me;
11. }
12. }
13. }

The text warns the code will not compile because:

unreported exception MyException; must be caught or declared to be thrown
doStuff();
^

Is this because MyException is declared to be thrown in doStuff (line 5) but not declared or handled in someMethod and/or is it because MyException is caught in doStuff (line 9) and again thrown in line 10?

Also, in line 10, me is thrown instead of MyException. I thought the exception name and not the object reference is to be thrown?

Thanks for your help.

JerryB.
Anthony Watson
Ranch Hand

Joined: Sep 25, 2003
Posts: 327
Any time a method declares that it throws a checked exception in its signature, such as "void doStuff() throws MyException" the exception must be caught by the code that calls the method. This is true even if doStuff had an empty implementation and could not possibly throw MyException.

There are two types of exceptions - checked and unchecked. Checked exceptions extend Exception, whereas unchecked exceptions extend RuntimeException. Checked exceptions must be caught but unchecked exceptions don't have to be.

Also, you can throw anything that extends Throwable. This means that you can do both "throw new Exception()" and "throw me." You would rethrow the original exception, "me" when you want to examine what the original cause of the problem was. Throwing a new exception erases all the details of the original problem.


Anthony W.<br />MCP, SCJP 1.4, SCJD, SCWCD 1.3, SCWCD 1.4, SCBCD
Robert Miller
Ranch Hand

Joined: Jun 18, 2004
Posts: 56
Originally posted by Jerry Bustamente:
I need some help understanding an example I found in Sierra Bates. On page 252 they list an exam watch warning to make sure handle and declare are handled properly.

The code is as follows:

1. class MyException extends Exception {
2. void someMethod () {
3. do stuff();
4. }
5. void doStuff() throws MyException {
6. try {
7. throw new MyException();
8. }
9. catch (MyException me) {
10. throw me;
11. }
12. }
13. }

The text warns the code will not compile because:

unreported exception MyException; must be caught or declared to be thrown
doStuff();
^

Is this because MyException is declared to be thrown in doStuff (line 5) but not declared or handled in someMethod and/or is it because MyException is caught in doStuff (line 9) and again thrown in line 10?

Also, in line 10, me is thrown instead of MyException. I thought the exception name and not the object reference is to be thrown?

Thanks for your help.

JerryB.


The above code will not compile because doStuff() is declared as throwing MyException, but someMethod(), which calls doStuff(), neither handles MyException nor declares itself as throwing it. Since it doesn't catch MyException an instance of MyException must be thrown out of someMethod(), but since it is not declared code which calls someMethod() doesn't know it might get MyException thrown to it. Therefore, the compiler doesn't allow it.

I am assuming the code on line 3 is a typo.

It is perfectly legitimate for a catch block to rethrow an exception it has just caught, or to throw a completely different exception.

Robert
SCJP 1.4
Pal Sudarshan
Ranch Hand

Joined: Jun 10, 2004
Posts: 52
Hi,

You get an error because someMethod() must handle or throw MyException. Your doStuff method declares that it will throw MyException, so there is a contract between whoever calls this method that it will handle the thrown exception. It doesn't matter whether the doStuff throws or handles the exception. The compiler simply says "hey doStuff is saying that it is going to throw MyException, so someMethod should catch it." Compiler is not smart enough to see whether MyException is actually thrown or not. It is possible to catch MyException in doStuff() but the compiler still wouldn't care. If a method says it will throw exception, the compiler believes it.


Jerry Bustamente
Ranch Hand

Joined: May 24, 2004
Posts: 90
Thank you Anthony, Robert, and Pal for your guidance. Your answers cleared that up for me. And yes, that was a typo in line 3. :-)

Sincerely,

JerryB
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: throwing Exceptions
 
Similar Threads
Checked Exceptions are Checked by Compiler to see if they are Handled or Declared
Exception question
Exception
exception handling
Exception handling throw and throws