• 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

Quick question on exception

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following try/catch block:
try{
statement 1; // may throw Exception1 object
statement 2; // may throw Exception2 object
statement 3; // may throw Exception3 object
}
catch( Exception1 ex1 )
{
.........
}
catch( Exception2 ex2 )
{
............
}
finally
{
statement4;
}
statement5;

(a) Under what circumstances will statement5 be executed?
(b) Under what circumstances will statement4 be executed?
(c) Under what circumstances will statement3 be executed?
I'm revising for my exam and I'm still unclear about some parts here. Hope someone can explain. thanks
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Statement 5 will be executed if no uncaught exception arises.
Statement 4 will be executed if any exception arises or not.
Statement 3 will be executed only if neither of the statements above it throw an exeption.

[This message has been edited by Cindy Glass (edited April 29, 2001).]
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yeah, RyanY,
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at "JavaRanch Naming Policy" . We require names to have at least two words (not just letters), separated by a space, and strongly recommend that you use your full real name. We want to get to know you as a Professional. Please choose a new name which meets the requirements and re-register.

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try{
statement 1; // may throw Exception1 object
statement 2; // may throw Exception2 object
statement 3; // may throw Exception3 object
}
catch( Exception1 ex1 ){.........}
catch( Exception2 ex2 ){.........}
finally{statement4;}
statement5;
Cindy don't you think it should be like this. (Please pardon me if I am wrong and let me know.)
If exception1 is thrown, it is caught and then finally block is executed and the execution continues normally. So statement 1,4 and 5 get executed.
If exception1 is NOT thrown and exception2 is thrown, again it is caught and then finally block is executed and the program continues normally. So the statement 1,2,4 and 5 get executed.
If neither exception1 nor exception2 is thrown but exception3 is thrown, it is not caught here and is propagated up to the calling method but still finally block gets executed. So statement 1,2,3 and 4 get executed (statement5 does not execute, control is passed to the calling method as exception3 is not caught here. This method should include "throws excception3" clause in its signature as the calling method has to be informed to handle exception3).
If no exception is thrown still finally block gets executed and then program progresses normally. In this case statement 1,2,3,4 and 5 all get executed.
Hope I am not wrong.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a Java Newbie, so I'm curious to see how my reply stacks up to the Expert's replies.
To test this, I wrote a little program that forces an ArrayIndexOutOfBoundsException.

Here's what I got:
0
1
2
3
4
ArrayIndexOutOfBoundsException
Got to finally statement
Gets here for normal execution?
So -- based upon that....
Statement #3 will only execute if there was NO Exception#1 or Exception#2.
Statement #4 will always execute, even if an Exception has occurred - caught or uncaught. (Makes sense, since the finally block is where you'd put your file.close() stuff.)
And, if you've caught your Exceptions, it looks like the program continues on -- which surprised me! So, in your excerpt, Statement #5 will be executed if you've caught your previous exceptions correctly.
BUT -- you don't catch Exception #3 anywhere - at least not in the snippet of code you posted. So, if you end up with that exception thrown, your program jumps to the finally block, executes statement #4 and stops.
OK, let me know how I did!
Susan

[This message has been edited by Susan Delph (edited April 28, 2001).]
 
Susan Delph
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Atul,
I just read your reply -- and based upon my test program's result, I agree with it.
You gave a very clear explanation!
Susan
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Glass, Heres my new Nick. BEtter?
Thanks everyone for the detailed reply ! =D
 
atul kashyap
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Susan.
If the exception thrown is caught in the method itself then the program continues normally.
If the exception is not caught the program does not stop there, the exception is propagated up the hierarchy:-
If the exception thrown is not caught in the method (and it is NOT a subclass of RuntimeException) then it has to be declared in throws clause in the method signature, so that the calling method comes to know that the exception is thrown but not caught and it has to handle that exception.
If the calling method also does NOT handle the exception then it is handled by the JVM in its own way - giving a message along with the stack trace from where the exception was thrown.
Atul
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys. You are right (rushing again - gotta stop that). I edited my post so as not to confuse anyone more.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IF exception one is thrown and is caught ,then stmnt 2 executes or not?
 
Ryan Yeap
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yasir:
From what i have learnt from this post. I guess if exception1 is thrown and caught then statement 2 and 3 will not be executed....
and i right to say that? =D
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct. Per the JLS:


The control transfer that occurs when an exception is thrown causes abrupt completion of expressions (�15.6) and statements (�14.1) until a catch clause is encountered that can handle the exception; execution then continues by executing the block of that catch clause. The code that caused the exception is never resumed.


Which is why it is generally a bad idea to stack up multiple commands in the same try block. You should just put the ONE statement that is likely to throw an exception in each try block, but you can have lot's of try blocks with catches.
[This message has been edited by Cindy Glass (edited April 30, 2001).]
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this 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