• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception handling  RSS feed

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Test
{
public static void main(String args[])throws Exception
{
try
{
int x=5/0;
}
catch(IllegalArgumentException e)
{
System.out.println("IllegalArgumentException");
}
catch(ArithmeticException e)
{
System.out.println("ArithmeticException");
return;//if u use return the Last Line wont execute
}
catch(Exception e)
{
System.out.println("Exception");
}
finally
{
System.out.println("In finally");
}
System.out.println("Last Line");

}


}


here i am using return inside the catch block .by using this i am unable to display the Last Line can any one explain why it happens like that???

and one more doubt if i wont use return why it displays Last Line ?? actually the program execution must halt at arithmetic exception catch block and it must then display finally.. why it is agian displaying Last Line??

lease can any one clear my simple doubt
thanking you in advance
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and can i throw unchecked or checked exceptions being inside catch block?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a return statement in your catch block, then any code in the finally block will be executed and then the method will return.
If you don't have a return statement in your catch block, then any code in the finally block will be executed and then the next line of code in your method after the finally block will be executed. If you want the method to stop after catching an exception then you have to have a return in your catch block.

You can throw any type of exception in your catch block. If it is a checked exception then the method signature will need to include a throws statement for that exception or one of its parent classes.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Joanne Neal:
If you want the method to stop after catching an exception then you have to have a return in your catch block.




Wrong

finally will always be executed even if you return it from catch or from try.

Check this code....



Output...

In catch
In finally


Regards


Naseem
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

and can i throw unchecked or checked exceptions being inside catch block?


Yes in general you can throw any Throwable instance or its subclass in catch.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Naseem Khan:
Wrong

finally will always be executed even if you return it from catch or from try.


Well if you had read the whole post you would have noticed I had already said that in the first sentence. I don't like to repeat myself
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!