异常机制
什么是异常
简单分类
异常体系结构
Error
Exception
Java异常处理机制
处理异常
package exception;
public class Test2 {
public static void main(String[] args) {
int a = 1;
int b = 0;
try {
System.out.println(a/b);
} catch (Exception e) {
e.printStackTrace();
}finally {
}
}
}
package exception;
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 0;
try {
System.out.println(a/b);
}catch (Error e){
System.out.println("Error");
}catch (Exception e){
System.out.println("Exception");
}catch (Throwable t){
System.out.println("Throwable");
}finally {
System.out.println("finally");
}
}
public void a(){b();}
public void b(){a();}
}
package exception;
public class Test {
public static void main(String[] args) {
try {
new Test().test(1,0);
} catch (Exception e) {
e.printStackTrace();
}
}
public void test(int a,int b) throws ArithmeticException{
if (b==0){
throw new ArithmeticException();
}
System.out.println(a/b);
}
}
自定义异常
总结 重点