• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

"Handle or Declare" Rule for Checked Exceptions

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.....
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ 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

 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These are wonderful, helpful responses. I'm reading through them to understand them better. Will respond soon.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ 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
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 was the best of times. It was the worst of times. It was a tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic