Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

throwing Exceptions

 
Jerry Bustamente
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Robert Miller
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic