File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes exception ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "exception ?" Watch "exception ?" New topic

exception ?

Kameshwar Prasad

Joined: Dec 04, 2007
Posts: 12
the fallowing first example is from jxam or jargon the out put does not include " End processing " why ?
but second example from Khalid and Rolf example gives an out put which includes " Returning from division." which is at the same place as "End processing" of above example ?

first example :-

public class Base6{

private void test() {

try {
String a = null;
String b = null;
// Complex processing

throw new MalformedURLException("test");
if(b ==null)
throw new EOFException("test");
// Complex processing

System.out.println("End of try block");

catch (MalformedURLException e) {
System.out.println("Caught MalformedURLException");
catch (EOFException e) {
System.out.println("Caught EOFException");

System.out.println("End processing");

static public void main(String[] a) {
new Base6().test();


// Select all valid answers
// a) End of try block
//corect ans b) Caught MalformedURLException
// c) Caught EOFException
// d) End processing
// e) None
second example from Khalid and Rolf :-
class DivisionByZeroException extends Exception {
DivisionByZeroException(String msg) { super(msg); }
public class DivisionByZero5 {
public void division() {
int num1 = 10;
int num2 = 0;
try { // (1)
if (num2 == 0) throw new DivisionByZeroException("/ by 0"); // (2)
System.out.println(num1 + " / " + num2 + " = " +
(num1 / num2)); // (3)
} catch (DivisionByZeroException e) { // (4)
System.out.println("Dealt with " + e);
} finally { // (5)
System.out.println("Finally done.");
System.out.println("Returning from division."); // (6)
public static void main(String args[]) {
new DivisionByZero5().division();
System.out.println("Returning from main."); // (7)
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
Hie kameshwar,
I think you need to recall that "return" is a "Flow Control Transfer" statement.Control transfer statements are those statements that transfer the flow of control from the current position to some other position. Return, transfers the flow from a method to where the method was called from.So the presence of "return" in the catch block of the first example makes control to move from this method altogether, and thus the last line is not executed.Notice that "return" is not present in the catch block of the second example. Try to verify this by removing "return" from the catch block of the first example and see if it behaves differently.
It is sorta covered in the JavaRanch Style Guide.
subject: exception ?
It's not a secret anymore!