Java program on Exceptions with getMessage() method Example 2
PROGRAM:
public class GetMessageClass {public static void main(String[] args) {
System.out.println("In main");
Demo d = new Demo();
try {
d.m1();
} catch (Exception e) {
System.out.println("Exception caught in main: " + e.getMessage());
}
}
}
class Demo {
void m1() {
System.out.println("In m1");
try {
m2();
} catch (Exception e) {
System.out.println("Exception caught in m1: " + e.getMessage());
throw e;
}
}
void m2() {
System.out.println("In m2");
try {
int a = 5, b = 0, c = a / b;
System.out.println(c);
} catch (Exception e) {
System.out.println("Exception caught in m2: " + e.getMessage());
throw e;
}
}
}
OUTPUT:
In mainIn m1
In m2
Exception caught in m2: / by zero
Exception caught in m1: / by zero
Exception caught in main: / by zero