File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

try catch finally blocks

 
sankar
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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):
 
sankar
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 289
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic