File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception Handling" Watch "Exception Handling" New topic

Exception Handling

Hari mylavar

Joined: Apr 14, 2009
Posts: 3
Hello Java Experts - Could you help me understand why the line after the catch block is executed. My understanding is after the exception - the flow goes to the catch block and does not execute beyond unless there is a finally block. Correct my understanding. Thank you -

public class NoExceptionHandling{
public static void main(String[] args){
int x=2, y=4, z=0;
y=x/z; // division by 0
System.out.println("This line is not executed because of the exception");
}// end try
catch (Exception e){
}//end of catch block
System.out.println("Here we print the value of x/z = " + y); // I expected that this line will not be executed. See the output
}// end main method
}// end of class NoExceptionHandling
C:\ForAni\Java>java NoExceptionHandling
Here we print the value of x/z = 4
jittu goud
Ranch Hand

Joined: Mar 30, 2007
Posts: 46
welcome to javaranch could you please embed your code in [CODE] tags ....
jittu goud
Ranch Hand

Joined: Mar 30, 2007
Posts: 46
the whole point of try catch is to recover your program from the known errors/exception.... in a program with out try catch would probably crash....

so if exception occours in try block ...the code with in the try block after the exception is skipped....and the relavant action is taken in cathe...but the rest of the program should work normally....
Hari mylavar

Joined: Apr 14, 2009
Posts: 3
Thank you Jittu.

Note: I will use the code tag next time when I post. Thank you. -Hari
I agree. Here's the link:
subject: Exception Handling
Similar Threads
Notrajon Mock Question 18
Runtime behavior after command-line invocation
precedence prob. again..this time 4rm RHE...very urgent
K&B Book for Scjp 6 - Chapter 5 question 8 doubt
Some Runtime vs. Compile Time SCJP 6.0 Mock Exam Questions