• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

try finally without a catch:  RSS feed

 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I thought try block immediately followed by a finally block wouldn't mean that the Exception is handled and that it should be declared. And I had also tried out some programs and results suggeated the same.

But here in this code, I thought the test2() method should declare that it throws the exception. Well, it doesn't. And it compiles just fine...

Can anyone please clarify...

Src: javabeat




OPTIONS:
1. The program won't compile because of compilation errors.
2. The program will throw Exception at run-time.
3. The program will print 6.
4. The program will output 9.
5. The program will display 3.

ANS ption 5
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the answer is 3. What is your confusion.
Am feeling
Reaching 300
[ January 17, 2008: Message edited by: Deepak Jain ]
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vishwanath Murthi,


Yes the answer is correct. 3 is what getting returned and displayed.


I thought try block immediately followed by a finally block wouldn't mean that the Exception is handled and that it should be declared. And I had also tried out some programs and results suggeated the same.

But here in this code, I thought the test2() method should declare that it throws the exception. Well, it doesn't. And it compiles just fine...



Here you have mistaken. The point is that,

  • If you handle the exception (using try/catch blocks), you do NOT need to declare the throws statement. That means, you intimate that you yourself handle the exceptions in the method.
  • on the other hand, if you don't handle the exception but simply throw an exception, you need to declare that in the method signature using throws statement.


  • As such the test() method handles the exception it its try/catch block, there is NO need for it to have throws clause. So the entire program compiles and runs just fine.

    Hope you are clear with the flow of execution
     
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The answer will be three as the finally block is executed any ways.

    it doesnot make any difference whether it goes to that loop or not

    Hope that helps!
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You may please go through this code for your further explanation



    Hope this helps!
     
    Vishwanath Krishnamurthi
    Ranch Hand
    Posts: 331
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi guys,

    The rule says "catch or declare" right...
    My confusion lies here:

    When we use a try block followed by a finally block without any catch block:
    any exception if any is not actually caught and just passed to the invoking method..rite?

    So we haven't caught the exception. So we gotto declare the exception atleast, right?

    And the code below,




    gives an error saying that there should be a throws clause there.... (in LLL)

    Well, so try-finally-(no-catch) block requires a throws declaration rite..?

    The test2() method in the prev code has a try-finally-(no-catch) block and does not have a throws declaration???

    Thats where I am getting confused.. Why isn't this code givin a compiler-error...


    (I can see that o/p must be three if it runs successfully... and it does)
     
    Vishwanath Krishnamurthi
    Ranch Hand
    Posts: 331
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    am very sorry... in the first post I meant to say

    I thought try block immediately followed by a finally block, without any catch block at all, wouldn't mean that the Exception is handled and that it should be declared.

    but typed


    I thought try block immediately followed by a finally block wouldn't mean that the Exception is handled and that it should be declared.

     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Originally posted by Vishwanath Murthi:
    ...
    When we use a try block followed by a finally block without any catch block:
    any exception if any is not actually caught and just passed to the invoking method..rite?

    So we haven't caught the exception. So we gotto declare the exception atleast, right?



    Yes, but it does have a "return" statement.

    Try the code with "try-finally with return" and "try-finally without return". for the second one, you need to have a throws clause!

    That's the difference
     
    Vishwanath Krishnamurthi
    Ranch Hand
    Posts: 331
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,

    Thanks a lot.
     
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi friends,

    Raghavan's point is right and I want to elaborate that,

    1) If method returns nothing and if you throw an Exception in a try block that does not handle by the catch block and not even handle by the caller then compiler will raise an error stating unhandled exception.

    2) If method returns some value and if you throw an Exception in a try block that does not handle by the catch block, you do not return any value in your finally block and not even handle by the caller then compiler will raise an error stating unhandled exception.

    3) If method returns some value and if you throw an Exception in a try block that does not handle by the catch block and not even handle by the caller but this time you return some value from your finally block then compiler will acept the things knowing that error occurs or not finally block is going to execute anyhow and will return some value, so no point of handling the error and will result in the value return by the fianlly block.


    hope it clears now.

    Alpesh
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Howdy Alpesh Vesuwala ,

    Welcome to JavaRanch
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Originally posted by Alpesh Vesuwala:

    ....3) If method returns some value and if you throw an Exception in a try block that does not handle by the catch block and not even handle by the caller but this time you return some value from your finally block then compiler will acept the things knowing that error occurs or not finally block is going to execute anyhow and will return some value, so no point of handling the error and will result in the value return by the fianlly block.



    That is a nice explanation Alpesh!
     
    Vishwanath Krishnamurthi
    Ranch Hand
    Posts: 331
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,

    you return some value from your finally block then compiler will acept the things knowing that error occurs or not finally block is going to execute anyhow and will return some value, so no point of handling the error and will result in the value return by the fianlly block.



    Thanks a lot for the clarification..
    So I see that a finally block can also be used to return a default-value in case an exception occurs...

    I tried this..


    Thanks...
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That's great viswanathan
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!