File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark ""Handle or Declare" Rule for Checked Exceptions" Watch ""Handle or Declare" Rule for Checked Exceptions" New topic
Author

"Handle or Declare" Rule for Checked Exceptions

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Code based on Sierra/Bates, Chapter 5



Of course, I get the error

MyException.java:5: unreported exception MyException; must be caught or declared to be thrown
doStuff();
^
1 error



I am unable to understand the book's explanation as to why this doesn't work. I would think that since doStuff() says it throws MyException, and then has a try-catch block in the code, this would work because the exception has been checked.

Dear oh dear.....

Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Well your doStuff does declare that it might throw MyException. It doesn't matter to the compiler what the code in your doStuff method is. By declaring MyException in throws clause of doStuff, you are telling the compiler to make sure that any calls to doStuff method must handle MyException...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Dear Sandra,

In above code , doStuff method declares that it throws MyException, so in main method you have to follow declare-handle law.

You can do the following



OR



SCJP 6.0 96%

(Connecting the Dots ....)
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
@ Sahil: both code snippits work. I do notice that the catch block is missing throw me;, which is what Sierra/Bates included AND which is the line causing the error...

@ Ankit: So from what I understand, by having the catch block throw back the exception, i.e. throw me this itself is an error, and probably a trick the SCJP testers will use on us? Or if, throw me is legitimate, how can the main function handle this exception, I tried the below code, and I get errors:





Error:

exception MyException is never thrown in body of corresponding try statement
catch (MyException me){
^
1 error

Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

throwing the exception in the catch block is called as re-throwing the exception
and when we do this, we have to declare that the method throws exception
but if we do not do this then we get the error that the exception is unreported and must be caught or declared to be thrown
hence if we do the following


this code does not compile as the exception thrown by the catch block is not caught or declared to be thrown
but when we do following

then the code compiles fine and runs
hope this helps

and there is a little interesting thing
when we write

the compiler also says that
F:\Java\Javaranch problems>javac MyException.java
MyException.java:9: exception MyException is never thrown in body of correspondi
ng try statement
catch (MyException me){
^
MyException.java:19: unreported exception MyException; must be caught or declare
d to be thrown
throw me;
^
2 errors


now second error is ok, we got solution on this but what about first?
see
it says that MyException is not thrown in the body of corresponding try statement even if we are calling the doStuff() method , but we know that the method calling and all the execution is done at the run time and the compile time, but why does compiler says it, this is because we are catching the MyException that is a checked exception even if there is no possibility that the exception is thrown at runtime (this is because at this point the compiler does not know it, as we have not DECLARED the method throws exception)
but when we write that method throws the exception as follows

then in the try block, the compiler knows that calling the method may cause an Exception and hence it does not give error when we write catch block for that

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Hello Sandra ,

throwing an Exception is a valid JAVA statement and hence could be incorporated anywhere in a method (instance method or static method) i.e. in a try block, catch block, and even finally block, no problem in this as far as it is in some method.

Next thing is that , when certain method is throwing any exception (Risky Method), calling method (which is calling risky method) has to handle it , if calling method does not want to handle it , it can delegate its responsibility to its calling method. This rule is called Handle or declare.

Now let me take your program and lets try to fix the bug.



In above code you are trying to call a "doStuff" method in main method and doStuff method declare that it throws MyException. So you need to follow handle-declare in main() method , i want to say it again, in main method() you are to handle or declare, because in main() method you are calling something which may be risky i.e. doStuff(). So let us fix this bug , and i am showing you the both ways of doing this as follows.

1. Handle
2. Declare or delegate.

1. Handling Risky method in main()




2. Declare or delegate handling exception to calling method of main by declaring in method signature.

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
These are wonderful, helpful responses. I'm reading through them to understand them better. Will respond soon.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
Hey Sandra,

Let's take a closer look at this code...



The first idea is to remember is that the ONLY exceptions that are considered "handled" are those that occur within a try block. In this code two exceptions are created. The first one is created in the try block, and is "handled" by the catch. BUT, the catch block throws ANOTHER exception. This one is not handled!!! The second exception is dealt with by being "declared" in doStuff()'s signature. So, any method that wants to invoke doStuff() needs to either handle or declare the exception that doStuff() *might* throw.

Based on this, can you explain two ways that you could change main() so that it can legally invoke doStuff()?

hth,

Bert

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
@ Bert Bates: From my understanding, there are two ways that main can be changed so that it can legally invoke doStuff()

Code # 1: Include doStuff() in try-catch block within main() method. Handle the exception.




Code # 2: Declare that main throws MyException and delegate the handling of the exception to doStuff().



However, I am still stuck here. When I run the second code, it gives the following:

Exception in thread "main" MyException
at MyException.doStuff(MyException.java:10)
at MyException.main(MyException.java:4)


It seems that the doStuff() needs to be changed as well to actually handle the error in its catch block. Please see following code:






Please advise.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
Hey Sandra,

Your code:


First off, you're correct about the two approaches you could take in main(), handle or declare... hooray!

Now the question I have for you is this:

Assuming that you're not going to change doStuff(), and that it's all about what you do in main(), what are the implications of these two approaches?
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Bert Bates wrote:


First off, you're correct about the two approaches you could take in main(), handle or declare... hooray!

Now the question I have for you is this:

Assuming that you're not going to change doStuff(), and that it's all about what you do in main(), what are the implications of these two approaches?


Implications? Wow, let me guess...
I'm thinking modularity, especially if there are a team of programmers. If one person is writing main() and the other is writing doStuff(), how does the person who is writing main() know that the person writing doStuff() will handle the exception, i.e. without throwing it back?

Can you give me a hint?
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
Hey Sandra,

You said:


Implications? Wow, let me guess...
I'm thinking modularity, especially if there are a team of programmers. If one person is writing main() and the other is writing doStuff(), how does the person who is writing main() know that the person writing doStuff() will handle the exception, i.e. without throwing it back?

Can you give me a hint?


Sorry, let me be more specific:

First off, there is a mistake in what you just said... Tell me this, how *would* the guy writing main() "know" what doStuff() might do? Hmmm....

Second, if you're writing main(), what's acceptable behavior for main()? For instance it might be acceptable for a main() that's used during testing to behave differently than a main() that's meant for a final application...

Does that help?
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Bert Bates wrote:Hey Sandra,

You said:


Implications? Wow, let me guess...
I'm thinking modularity, especially if there are a team of programmers. If one person is writing main() and the other is writing doStuff(), how does the person who is writing main() know that the person writing doStuff() will handle the exception, i.e. without throwing it back?

Can you give me a hint?


Sorry, let me be more specific:

First off, there is a mistake in what you just said... Tell me this, how *would* the guy writing main() "know" what doStuff() might do? Hmmm....

Second, if you're writing main(), what's acceptable behavior for main()? For instance it might be acceptable for a main() that's used during testing to behave differently than a main() that's meant for a final application...

Does that help?


Well, I was thinking top of my head as I have lot of SCJP studying to do.


Now that I think of it, main() is the entry point where execution of the program starts, and it delegates tasks to other methods in the program.

HELP!
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
Can you think of a situation where it might be good for main() to declare an exception - that means that if an exception is thrown somewhere your program will fail?

Can you think of a situation where it might be good for main() to handle an exception?

Hint - I can think if good reasons for both!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: "Handle or Declare" Rule for Checked Exceptions
 
Similar Threads
Exception question
throw exception question
Exceptions doubt
checked exception
Concept on coding for Exception Handling