File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception and finally

 
Rakesh Ne
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If I compile and run the above code, "3" is printed. Can somebody please explain :
1. Why is a compilation error not thrown since method() is not throwing the Exception ?
2. A new Exception is thrown in catch block. How does "3" get printed since the that Exception is not handled ?

Thanks in advance

Rakesh N
 
Remko Strating
Ranch Hand
Posts: 893
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you have a return statement in your finally block. If you remove the return statement your code will not compile.
A return statement in a finally block will hide your exceptions. So it is not wise to do so.

 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, if you "return" or "throw" in a "finally" block, any pending exception is lost. Therefore, you should never "return" or "throw" in a "finally" block.

It is pretty easy to remember not to use "return" or "throw" in a "finally". But it is also necessary to ensure that any method calls in your "finally" cannot reasonably throw an exception, because that exception would hide the pending exception.

To do this: -

  • Keep code in "finally" as simple as possible
  • Use try..catch, inside the "finally", to handle any exceptions that occur. Usually, there is little you can do beyond logging the exception, as you already have another exception pending.

  •  
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Welcome to JavaRanch!

    Here's another way to look at this: An exception means that there's some problem preventing your code from proceeding as intended, so it breaks out of the method body and passes control elsewhere (the catch block). However, if you place a return statement in the finally block, then your method ends up returning normally, and execution continues from where the method was called, as if no exception were thrown.

    A finally block is for code that should execute in all cases -- whether or not an exception is thrown.
     
    Rakesh Ne
    Greenhorn
    Posts: 14
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for your replies.
    I am still in a confusion over the compilation error

    Even though we have the catch block throwing Exception, the method signature does not have throws Exception
    public static int method(){...}

    Why does the compiler not complain ?

    My guess - The code will never reach that stage. Hence it is supressed.
     
    Jesper de Jong
    Java Cowboy
    Saloon Keeper
    Pie
    Posts: 15150
    31
    Android IntelliJ IDE Java Scala Spring
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    "Why does the compiler not complain ?

    My guess - The code will never reach that stage. Hence it is supressed."


    You've got it right, no need to be confused anymore.
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic