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

Try Catch Finally - Voodoo Question

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I've another question, I've googled this one and fiddled with the code till I'm blue in the face but I'm still confused.

With regard to the code below, I thought flow would be as follows.

main()->raid()->try->Exception()->catch->Arithmetic Exception->finally

But I was wrong, the arithmetic exception is not reported, if I comment out the finally an arithmetic exception is thrown.

To me it appears this may happen because the arithmetic exception is unchecked and the finally must run therefore the runtime exception is ignored ?

Anyone able to confirm this or set me straight please?

Thanks in advance,

Jason



Output

[ February 09, 2006: Message edited by: Jason Keating ]
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
return in finally clause nullify exception
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

U r flow of execution is very correct,

main()->raid()->try->Exception()->catch->Arithmetic Exception->finally

But here look at the one thing, though in catch block arithmetic exception is thrown, control goes to finally block, as it must be executed in any condition. And in the finally block there is return statement, so whenever there is return statement in finally block all the previously thrown exceptions are ignored & the corresponding o/p i.e "peace" is printed.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The return statement outclasses the other return statements. As a result the return statement is executed[That is equivalent to the normal termination of the function]. The exception is not seen. You will get exception even if you have the finally block and not any return statement. Two things to consider is that "its neither the return nor the finally that does the work, but infact both does the work".
 
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

so whenever there is return statement in finally block all the previously thrown exceptions are ignored & the corresponding o/p i.e "peace" is printed.



Not always this happen. If a unchecked exception is thrown in try block, that will be printed after executing the finally block.

Regards,
Narendranath
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Naren Chivukula:
Hi,
Not always this happen. If a unchecked exception is thrown in try block, that will be printed after executing the finally block.



Hmmm, I don't think this is true. Consider the following code below:


The output is "Peace" (without the quotes) and the RunTimeException error is not displayed.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swathi got it right,thats the correct explanation

RunTimeException is generated , we are already in catch so execution goes to finally ,where "Peace" gets returned.
 
Naren Chivukula
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Hmmm, I don't think this is true. Consider the following code below:



As you are catching the exception in the catch block, you can't get the exception thrown after finally block executes. If you don't use catch block over there, then certainly system throws RuntimeException after executing finally block code.

Regards,
Narendranath
 
Chris Allen
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nare,

You stated the following:


If a unchecked exception is thrown in try block, that will be printed after executing the finally block.



All I wanted to indicate was that a Runtime exception will NOT ALWAYS be printed (as it was caught in a try block). You are correct that it will display if not caught by a catch block.

Perhaps we could agree that if you modified the statement to be "If an unchecked exception is thrown in a try block that is not caught by an exception clause, the runtime exception will be printed after executing the finally block".
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chris Allen:
You are correct that it will display if not caught by a catch block.



No, actually, he's not correct under any circumstances. Returning from a method inside a finally block will "cancel" all pending exceptions, caught or uncaught, whether there's a matching catch block or even no catch block at all. Try this:



Running this program produces no output.
 
Naren Chivukula
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Perhaps we could agree that if you modified the statement to be "If an unchecked exception is thrown in a try block that is not caught by an exception clause, the runtime exception will be printed after executing the finally block".



A java files never goes without catching unchecked exception. Compiler will not compile the java file unless you catch that unchecked exception!

No, actually, he's not correct under any circumstances. Returning from a method inside a finally block will "cancel" all pending exceptions, caught or uncaught, whether there's a matching catch block or even no catch block at all.


I too agree. If we have simple SOP statement in finally block, then the
RuntimeException thrown in try block will be printed on the console. I need to elaborately study on this...

Narendranath
-- Always try to write right statements
 
Do Re Mi Fa So La Tiny Ad
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic