wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes try catch finally blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "try catch finally blocks" Watch "try catch finally blocks" New topic
Author

try catch finally blocks

sankar
Greenhorn

Joined: Oct 02, 2000
Posts: 17
I am confused with this concept pls.share knowledge
try{
}catch(subexcep se){System.out.println("subexcep");}
}catch(exce e){System.out.println("excep");}
finally{System.out.println("finally");}
System.out.println("after finally");}
1.what happens if subexcep occurs?
2.if excep occurs & caught does the statement afterfinally gets executed?
3.if some other eception occurs which is not handled what happens?
any good points to remember on this topic
sankar
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Well Done. So this is just one more amguity that you have given me. There is a learning in your code.
In your code there is a method method1 that's overlaoded. So when you pass a byte variable to it, it is possible for this variable to be trapped by both the versions of the method due to arithmetic promotion.
Hence the ambiguity. To remove the ambiguity as to which varsion of the method you want to use just override it in the subclass.
Thanks again. Now I have 3 ways to create an amguity in Java.
sankar
Greenhorn

Joined: Oct 02, 2000
Posts: 17
But i didn't got any answer from ur explanation.May be i am stupid.Better explain step by step
thanks
sankar
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
When the code in a try block is executed,
follow these rules:
1) if exception occurs skip the code and jump straight to the specific exception, On its completion jump to the finally block and then to the statements that follow the finally block
This will not work if a System.exit(0) is seen in the catch block thus finally and the statements followed do not execute.
An example
class err
{
static String s=null;
static public void main(String[]p)
{
try
{
s.substring(5);
System.out.println(new err().s);
}
catch(NullPointerException e)
{
System.out.println("in");
}
catch(Exception r)
{
System.out.println("in ex");
r.printStackTrace();
}
finally
{
System.out.println("doing finally");
}
System.out.println("out");
}
}
gives the output
[b]
D:\>java err
in
doing finally
out
D:\>
[\b]

2) If there is no exception the each statement in the catch block is executed followed by everything in the finally block. Then the control is given to the lines below the finally block.
Moral finally is executed no matter whether an exception occurs or not except when a call to exit has been invoked
sgwbutcher
Ranch Hand

Joined: May 13, 2000
Posts: 56
Hello sankar,
Here is a reworked version of the code you provided that should compile and run. Try using:
java Test 1
java Test 2
java Test 3
java Test 4
at the command line and see what happens. This should answer your questions and give you a better understanding.
Note that an uncaught checked exception can't be thrown because the compiler would require you to catch it or declare it in the throws clause of the test method...but you HAVE to handle it somewhere. That's why I throw an unchecked exception to show what happens if an uncaught exception occurs.
Best regards,
Steve Butcher
exceptionraised@aol.com

Your output should look something like this (except for java Test 3 where the actual output will depend on your environment):


-------------------------<BR>Steve<BR>sgwbutcher@aol.com<BR>http://hometown.aol.com/sgwbutcher/
sankar
Greenhorn

Joined: Oct 02, 2000
Posts: 17
Thanks Mr.Steve & Mr.lionel
your explanations & program helped me to clear my doubt.Shall i know from which place are u both???

sankar
sankar
Greenhorn

Joined: Oct 02, 2000
Posts: 17
Thanks Mr.Steve & Mr.lionel
your explanations & program helped me to clear my doubt.Shall i know from which place are u both???

sankar
if possible write to me
umasankarp@usa.net
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
Lionel,
In your explanation you have this statement :
------------------------------------------------------------
2) If there is no exception the each statement in the catch block is executed followed by everything in the finally block. Then the control is given to the lines below the finally block.
----------------------------------------------------------------
I suppose you meant to say each statement in the TRY block and not in the catch block as it reads above.If there is no exception thrown, the catch block will not execute, so control will flow from the try block to the finally block and then the rest of the code following the finally block.
Regards,
Herbert.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: try catch finally blocks