Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exception ?

 
Kameshwar Prasad
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :-
import java.io.*;
import java.net.*;

public class Base6{

private void test() {

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

if(a==null)
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");
return;
}
catch (EOFException e) {
System.out.println("Caught EOFException");
return;
}

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
Posts: 289
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Regards,
Herbert.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic