-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUserDefinedException.java
57 lines (49 loc) · 1.45 KB
/
UserDefinedException.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Checked Exception
*/
class MyException extends Exception {
MyException() {
}
MyException(String msg) {
super(msg);
}
}
/**
* Unchecked Exception
*/
class MyUncheckedException extends RuntimeException {
MyUncheckedException() {
}
MyUncheckedException(String msg) {
super(msg);
}
}
class UserDefinedExceptionDemo {
void demonstrateChecked() throws MyException {
throw new MyException("This is a checked user-defined exception");
}
void demonstrateUnchecked() {
throw new MyUncheckedException("This is an unchecked user-defined exception");
}
}
public class UserDefinedException {
public static void main(String[] args) throws MyException {
UserDefinedExceptionDemo test = new UserDefinedExceptionDemo();
// try {
// test.demonstrateChecked();
// } catch (MyException e) {
// e.printStackTrace();
// System.out.println("Printing from catch block of main() " + e);
// }
test.demonstrateChecked(); // Uncaught checked user-defined exception
System.out.println();
test.demonstrateUnchecked(); // Uncaught unchecked user-defined exception
try {
test.demonstrateUnchecked();
} catch (MyUncheckedException e) {
e.printStackTrace();
System.out.println("Printing from catch block of main() " + e);
}
System.out.println("End of main()");
}
}