-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCommonExceptionTypes.java
81 lines (61 loc) · 1.74 KB
/
CommonExceptionTypes.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package exceptionsAndAssertions;
/**
*
* @author chengfeili
* Jun 10, 2017 8:29:12 AM
*
*/
public class CommonExceptionTypes {
/**
* Runtime Exception (Unchecked Exception) It is unexpected, doesn't have to
* be handled or declared. It can be thrown by the programmer or by the JVM.
*
*/
public void runtimeException() {
// ArithmeticException
int n = 11 / 0;
// ArrayIndexOutOfBoundsException
int[] count = new int[3];
System.out.println(count[-1]);
// ClassCastException
// String type = "a";
// Integer num = (Integer) type; // Does not compile
String type = "a";
Object obj = type;
Integer num = (Integer) obj;
// NullPointerException
String name = null;
System.out.println(name.length());
// NumberFormatException
Integer.parseInt("abc");
// IllegalArgumentException
throw new IllegalArgumentException("must not be negative");
// ArrayStoreException
// DateTimeException
// MissingResourceException
// IllegalStateException
// UnsupportedOperationException
}
public void checkedException() {
// FileNotFoundException // subclass of IOExcepption
// IOException;
// ParseException Converting a String to a number
// NotSerializableException
// SQLException
}
public void errors() {
/**
* ExceptionInInitializerError Thrown by the JVM when a static
* initializer throws an exception and doesn’t handle it .
*/
/**
* StackOverflowError Thrown by the JVM when a method calls itself too
* many times (this is called infinite recursion because the method
* typically calls itself without end)
*/
/**
* NoClassDefFoundError Thrown by the JVM when a class that the code
* uses is available at compile time but not runtime
*/
}
}