Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Handling

 
Nina Binde
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there is an exception caught in a method, will the statements after the try/catch block execute (I know finally block always executes) or will the control go back to the caller method?
If the exception is not caught, I know for sure that the control goes back to the caller at the point of exception and no more statements are executed after that.
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nina, try this:

class ex
{
public static void main(String st[])
{
new ex1().exmethod1();
}
}

class ex1
{

void exmethod1()
{
try {
exmethod2();
} catch (Exception e)
{
System.out.println ("In Catch of Exception");
}

finally {
System.out.println ("In finally");
}

System.out.println ("After finally");
}

void exmethod2() throws Exception
{
throw new Exception();
}
}
 
Nina Binde
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys. Nitin, I tried the example and it is now clear to me. The question I asked was kind of stupid since if the statements after the try/catch block doesn't execute, then the purpose of the try/catch block is defeated!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic