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 try finally without  a catch: Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "try finally without  a catch: " Watch "try finally without  a catch: " New topic
Author

try finally without a catch:

Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 330
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


Blog
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Yes the answer is 3. What is your confusion.
Am feeling
Reaching 300
[ January 17, 2008: Message edited by: Deepak Jain ]

SCJP, SCWCD, SCBCD
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3287

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


    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    Pranob Samani
    Greenhorn

    Joined: Jan 16, 2008
    Posts: 11
    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

    Joined: Apr 20, 2006
    Posts: 3287

    You may please go through this code for your further explanation



    Hope this helps!
    Vishwanath Krishnamurthi
    Ranch Hand

    Joined: Jun 04, 2007
    Posts: 330
    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

    Joined: Jun 04, 2007
    Posts: 330
    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

    Joined: Apr 20, 2006
    Posts: 3287

    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

    Joined: Jun 04, 2007
    Posts: 330
    Hi,

    Thanks a lot.
    Alpesh Vesuwala
    Greenhorn

    Joined: Jan 16, 2008
    Posts: 9
    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


    Sun Certified Java Programmer<br />Sun Certified Mobile Application Developer
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3287

    Howdy Alpesh Vesuwala ,

    Welcome to JavaRanch
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3287

    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

    Joined: Jun 04, 2007
    Posts: 330
    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

    Joined: Apr 20, 2006
    Posts: 3287

    That's great viswanathan
     
     
    subject: try finally without a catch:
     
    developer file tools

    cast iron skillet 49er

    more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture