This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread 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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Handling
 
Similar Threads
Notrajon Mock Question 18
Runtime behavior after command-line invocation
precedence prob. again..this time 4rm RHE...very urgent
Some Runtime vs. Compile Time SCJP 6.0 Mock Exam Questions
K&B Book for Scjp 6 - Chapter 5 question 8 doubt