关键字:Error,Exception,错误、异常
1,Error是不需要捕捉,捕捉没有意义的严重错误。
2,发生Error的话,不管捕捉不捕捉,Java的这个线程都会退出了。
3,发生Exception的话,如果不捕捉,这个线程退出,捕捉的话这个线程继续。
4,退出的是发生Error和Exception的线程,不影响其他的线程。
5,显式的Exception,必需要捕捉,不然编译过不去。
6,5中说的还有不对的,Exception中有一个子类RuntimeException不需要捕捉,这类最典型的有ArithmeticException、IndexOutOfBoundsException、NullPointerException。
public class Tst {
public static void main(String[] args) {
System.out.println("main Start");
new A().start();
new B().start();
System.out.println("main End");
}
public static void exceptionMethod() throws Exception {
throw new Exception();
}
public static void errorMethod() {
throw new Error();
}
}
class A extends Thread {
public void run() {
System.out.println("A Start");
int a=1, c=0;
a=a/c;
System.out.println("A End");
}
}
class B extends Thread {
public void run() {
System.out.println("B Start");
throw new Error();
}
}