• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

exception ?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
}
}
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic