This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception Handling" Watch "Exception Handling" New topic
Author

Exception Handling

Hari mylavar
Greenhorn

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;
try{
y=x/z; // division by 0
System.out.println("This line is not executed because of the exception");
}// end try
catch (Exception e){
System.out.println("Exception!");
}//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
Exception!
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 ...it 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
Greenhorn

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

Note: I will use the code tag next time when I post. Thank you. -Hari
 
wood burning stoves
 
subject: Exception Handling