Skip to content

Commit 93d2862

Browse files
author
turingfly
committed
Exceptions
1 parent f07c3fb commit 93d2862

5 files changed

+362
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package exceptionsAndAssertions;
2+
3+
/**
4+
*
5+
* @author chengfeili
6+
* Jun 10, 2017 10:57:52 AM
7+
*
8+
*/
9+
class checked extends Exception{
10+
11+
}
12+
public class CallingMethodsThatThrowExceptions {
13+
// checked is a checked exception.
14+
// it must be handled or declared.
15+
16+
// method1: declare exception
17+
/*
18+
* public static void main(String[] args) throws checked {
19+
eat();
20+
}*/
21+
22+
// method2: handle exception
23+
public static void main(String[] args) {
24+
try {
25+
eat();
26+
} catch (checked e) {
27+
System.out.println("handle");
28+
}
29+
30+
// print an exception
31+
try {
32+
hop();
33+
} catch (Exception e) {
34+
System.out.println(e);
35+
System.out.println(e.getMessage());
36+
e.printStackTrace();
37+
/*
38+
* java.lang.RuntimeException: cannot hop
39+
* cannot hop
40+
* java.lang.RuntimeException: cannot hop
41+
* at trycatch.Handling.hop(Handling.java:15)
42+
* at trycatch.Handling.main(Handling.java:7)
43+
*/
44+
}
45+
}
46+
47+
private static void hop() {
48+
throw new RuntimeException("cannot hop");
49+
}
50+
51+
private static void eat() throws checked {
52+
53+
}
54+
55+
/**
56+
public void bad() {
57+
try {
58+
eatCarrot();
59+
} catch (checked e) { // DOES NOT COMPILE
60+
System.out.print("sad rabbit");
61+
}
62+
}
63+
*/
64+
65+
public void good() throws checked {
66+
eatCarrot();
67+
}
68+
69+
private static void eatCarrot() {
70+
}
71+
72+
/**
73+
* Java knows that eatCarrot() can’t throw a checked exception—which means
74+
* there’s no way for the catch block in bad() to be reached. In comparison,
75+
* good() is free to declare other exceptions.
76+
*/
77+
78+
// subclasses
79+
/**
80+
* When a class overrides a method from a superclass or implements a method
81+
* from an interface, it’s not allowed to add new checked exceptions to the
82+
* method signature
83+
*
84+
* A subclass is allowed to declare fewer exceptions than the superclass or
85+
* interface. This is legal because callers are already handling them.
86+
*
87+
* declare new runtime exceptions in a subclass method is that the
88+
* declaration is redundant. Methods are free to throw any runtime
89+
* exceptions they want without mentioning them in the method declaration.
90+
*/
91+
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package exceptionsAndAssertions;
2+
3+
/**
4+
*
5+
* @author chengfeili
6+
* Jun 10, 2017 7:49:39 AM
7+
*
8+
* 1. recognize if the exception is a checked or an unchecked exception.
9+
* 2. determine if any of the exceptions are subclasses of the others.
10+
*/
11+
class A extends RuntimeException {
12+
13+
}
14+
15+
class B extends RuntimeException {
16+
17+
}
18+
19+
class C extends B {
20+
21+
}
22+
23+
public class CatchingVariousTypesOfExceptions {
24+
25+
/**
26+
* If test() doesn't throw an exception, nothing is printed out. If catch A,
27+
* print "A". If catch B, print "B".
28+
*
29+
* A and B don't inherit from each other. So the order of the catch blocks
30+
* could be reversed.
31+
*/
32+
public void notSub() {
33+
try {
34+
test();
35+
} catch (A e) {
36+
System.out.print("A");
37+
} catch (B e) {
38+
System.out.print("B");
39+
}
40+
}
41+
42+
/**
43+
* C inherits from B.
44+
*
45+
* If the more specific C exception is thrown, the first catch block runs.
46+
* If not, Java checks if the superclass B exception is thrown and catches
47+
* it. This time, the order of the catch blocks does matter. The reverse
48+
* does not work and does not compile.
49+
*/
50+
public void sub() {
51+
try {
52+
test();
53+
} catch (C e) {
54+
System.out.print("C");
55+
} catch (B e) {
56+
System.out.print("B");
57+
}
58+
}
59+
60+
private void test() {
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package exceptionsAndAssertions;
2+
3+
import java.io.FileNotFoundException;
4+
import java.text.NumberFormat;
5+
6+
/**
7+
*
8+
* @author chengfeili
9+
* Jun 10, 2017 8:29:12 AM
10+
*
11+
*/
12+
public class CommonExceptionTypes {
13+
14+
/**
15+
* Runtime Exception (Unchecked Exception) It is unexpected, doesn't have to
16+
* be handled or declared. It can be thrown by the programmer or by the JVM.
17+
*
18+
*/
19+
public void runtimeException() {
20+
// ArithmeticException
21+
int n = 11 / 0;
22+
23+
// ArrayIndexOutOfBoundsException
24+
int[] count = new int[3];
25+
System.out.println(count[-1]);
26+
27+
// ClassCastException
28+
// String type = "a";
29+
// Integer num = (Integer) type; // Does not compile
30+
String type = "a";
31+
Object obj = type;
32+
Integer num = (Integer) obj;
33+
34+
// NullPointerException
35+
String name = null;
36+
System.out.println(name.length());
37+
38+
// NumberFormatException
39+
Integer.parseInt("abc");
40+
41+
// IllegalArgumentException
42+
throw new IllegalArgumentException("must not be negative");
43+
}
44+
45+
public void checkedException() {
46+
// FileNotFoundException // subclass of IOExcepption
47+
48+
// IOException;
49+
}
50+
51+
public void errors() {
52+
/**
53+
* ExceptionInInitializerError Thrown by the JVM when a static
54+
* initializer throws an exception and doesn’t handle it .
55+
*/
56+
57+
/**
58+
* StackOverflowError Thrown by the JVM when a method calls itself too
59+
* many times (this is called infinite recursion because the method
60+
* typically calls itself without end)
61+
*/
62+
63+
/**
64+
* NoClassDefFoundError Thrown by the JVM when a class that the code
65+
* uses is available at compile time but not runtime
66+
*/
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package exceptionsAndAssertions;
2+
3+
/**
4+
*
5+
* @author chengfeili
6+
* Jun 9, 2017 10:27:23 PM
7+
*
8+
* java.lang.Object -> java.lang.throwable -> (
9+
* (java.lang.Exception -> java.lang.RuntimeException) + java.lang.Error)
10+
*
11+
* 1. RuntimeException(Unchecked)
12+
*
13+
* 2. Checked Exception: includes Exception and all subclasses that
14+
* do not extend RuntimeException.
15+
* For checked exceptions, Java requires the code either handle them
16+
* ore declare them in the method signature.
17+
*
18+
* 3. Error
19+
*/
20+
public class ExceptionIntro {
21+
/**
22+
* Throw tells Java that you want to throw an Exception. throws simply
23+
* declares that the method might throw an Exception . It also might not
24+
*/
25+
public void fall() throws Exception {
26+
throw new Exception();
27+
}
28+
29+
/**
30+
* If any of the statements throw an exception that can be caught by the
31+
* exception type listed in the catch block, the try block stops running
32+
* and execution goes to the catch statement.
33+
*/
34+
public void TryStatement() {
35+
try{
36+
fall1();
37+
System.out.println("never get here");
38+
} catch (RuntimeException e) {
39+
getUp();
40+
} finally {
41+
/*
42+
* The finally block always executes, whether or not an exception
43+
* occurs in the try block.
44+
*
45+
* When System.exit is called in the try or catch block, finally
46+
* does not run.
47+
*/
48+
getUp();
49+
}
50+
}
51+
52+
private void getUp() {
53+
// TODO Auto-generated method stub
54+
55+
}
56+
57+
public void fall1() {
58+
throw new RuntimeException();
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package exceptionsAndAssertions;
2+
3+
import java.io.FileReader;
4+
import java.io.IOException;
5+
6+
/**
7+
*
8+
* @author chengfeili
9+
* Jun 10, 2017 8:04:26 AM
10+
*
11+
*/
12+
public class NestedException {
13+
14+
public void test() {
15+
FileReader reader = null;
16+
try {
17+
reader = read();
18+
} catch (IOException e) {
19+
try {
20+
if (reader != null)
21+
reader.close();
22+
} catch (IOException inner) {
23+
}
24+
}
25+
}
26+
27+
private FileReader read() throws IOException {
28+
// CODE GOES HERE
29+
return null;
30+
}
31+
32+
/**
33+
* Line 46 throws an exception, which is caught on line 47. The catch block
34+
* then throws an exception on line 48. If there were no finally block, the
35+
* exception from line 48 would be thrown. However, the finally block runs
36+
* after the try block.
37+
*
38+
* Since the finally block throws an exception of its own on line 50, this
39+
* one gets thrown. The exception from the catch block gets forgotten about.
40+
* This is why you often see another try / catch inside a finally block—to
41+
* make sure it doesn’t mask the exception from the catch block.
42+
*
43+
*/
44+
public void mask() throws Exception {
45+
try {
46+
throw new RuntimeException();
47+
} catch (RuntimeException e) {
48+
throw new RuntimeException();
49+
} finally {
50+
throw new Exception();
51+
}
52+
}
53+
54+
public String exceptions() {
55+
String result = "";
56+
String v = null;
57+
try {
58+
try {
59+
result += "before ";
60+
v.length();
61+
result += "after ";
62+
} catch (NullPointerException e) {
63+
result += "catch ";
64+
throw new RuntimeException();
65+
} finally {
66+
result += "finally ";
67+
throw new Exception();
68+
}
69+
} catch (Exception e) {
70+
result += "done ";
71+
}
72+
return result;
73+
}
74+
75+
public static void main(String[] args) {
76+
NestedException ne = new NestedException();
77+
ne.test();
78+
System.out.println(ne.exceptions()); // before catch finally done
79+
}
80+
}

0 commit comments

Comments
 (0)