So I understand the use of throwing an Exception so that the next level of a program will handle the Exception but what is re-throwing all about?
Re-throwing Exceptions? I don't understand
Started by thieflock, Feb 19 2008 02:59 PM
1 reply to this topic
#1
Posted 19 February 2008 - 02:59 PM
|
|
|
#2
Posted 20 February 2008 - 12:11 PM
Its useful if an error is thrown within a nested block of code, and you don't want to deal with it in the innermost block.
Also, if the inner level block can only partially handle the error, it can re-throw it so the higher level block can completely handle it.
Tough the inner level could throw it, and not deal with it at all.
This might help:
[HIGHLIGHT="Java5"]
import java.util.*;
import javax.swing.JOptionPane;
public class ThrowSample {
static Scanner console = new Scanner(System.in);
static int num1, num2, num3;
public static void main(String[] args) {
try {
System.out.println("Please input number 1:");
num1 = console.nextInt();
System.out.println("Please input number 2:");
num2 = console.nextInt();
try {
num3 = num1 / num2;
} catch (ArithmeticException ae) {
JOptionPane.showMessageDialog (null, "Division by 0 caught \nError will be thrown to the calling enviroment", "ERROR!", 0);
throw ae;
}
} catch (InputMismatchException ime) {
JOptionPane.showMessageDialog (null, "Not a number \nProgram Will Now Terminate", "ERROR!", 0);
} catch (ArithmeticException ae) {
JOptionPane.showMessageDialog (null, "Division by 0 has ben re-thrown, and caught \nIf i could i would deal with it here \nBut i cant, so program will terminate", "ERROR!", 0);
} catch (Exception e) {
JOptionPane.showMessageDialog (null, e.toString()+"\nProgram Will Now Terminate", "ERROR!", 0);
} finally {
System.out.println("exit();");
System.exit(0);
}
}
}
[/HIGHLIGHT]
If you enter a letter instead of a number, the calling method will handle it.
If one of the numbers is 0, well we can't divide two int's by zero can we? But i wont handle it in the inner try block.
So its re-thrown to the outer method. Which catches it, and shuts down.
Hope this has helped.
~Gabor
Also, if the inner level block can only partially handle the error, it can re-throw it so the higher level block can completely handle it.
Tough the inner level could throw it, and not deal with it at all.
This might help:
[HIGHLIGHT="Java5"]
import java.util.*;
import javax.swing.JOptionPane;
public class ThrowSample {
static Scanner console = new Scanner(System.in);
static int num1, num2, num3;
public static void main(String[] args) {
try {
System.out.println("Please input number 1:");
num1 = console.nextInt();
System.out.println("Please input number 2:");
num2 = console.nextInt();
try {
num3 = num1 / num2;
} catch (ArithmeticException ae) {
JOptionPane.showMessageDialog (null, "Division by 0 caught \nError will be thrown to the calling enviroment", "ERROR!", 0);
throw ae;
}
} catch (InputMismatchException ime) {
JOptionPane.showMessageDialog (null, "Not a number \nProgram Will Now Terminate", "ERROR!", 0);
} catch (ArithmeticException ae) {
JOptionPane.showMessageDialog (null, "Division by 0 has ben re-thrown, and caught \nIf i could i would deal with it here \nBut i cant, so program will terminate", "ERROR!", 0);
} catch (Exception e) {
JOptionPane.showMessageDialog (null, e.toString()+"\nProgram Will Now Terminate", "ERROR!", 0);
} finally {
System.out.println("exit();");
System.exit(0);
}
}
}
[/HIGHLIGHT]
If you enter a letter instead of a number, the calling method will handle it.
If one of the numbers is 0, well we can't divide two int's by zero can we? But i wont handle it in the inner try block.
So its re-thrown to the outer method. Which catches it, and shuts down.
Hope this has helped.
~Gabor
~Aristotle said:
It is the mark of an educated mind to entertain a tought without accepting it


Sign In
Create Account


Back to top









