Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Try commenting in and out different lines below
- // to see how Exceptions are handled.
- public class ExceptionTest {
- // Exceptions are just instances of the Exception class
- // or any subclass... we can make our own Exception subclasses
- // for Exceptions specific to a particular application.
- public static class LocalException extends Exception {
- public LocalException(String msg) { super(msg); }
- }
- public static double A() throws Exception {
- return B();
- }
- public static double B() throws Exception {
- double return_val = 50d;
- try {
- return_val = C();
- } catch (LocalException e) {
- return_val = 100d;
- //throw e;
- //throw new Exception("Modified Exception: " + e.getMessage());
- } finally {
- System.out.println("Always printed!");
- }
- return return_val;
- }
- // Try each of the following three return statements
- public static double C() throws Exception {
- throw new Exception("ERROR");
- //throw new LocalException("ERROR");
- //return 1.0;
- }
- public static void main(String[] args) {
- double val = 3d;
- try {
- val = A();
- System.out.println("Main Value: " + val);
- } catch (Exception e) {
- System.out.println("Main Exception: " + e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement