• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception and finally

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

  •  
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Java Cowboy
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Everybody! Do the Funky Monkey! Like this tiny ad!
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic