回顾昨天的内容
异常处理机制
1、抛出异常
2、捕获异常
3、异常处理的五个关键词
try 、catch、finally、throw、throws
public class text{
public static void main(String [] args){
int a = 1;
int b = 0;
try{// try监控区域
System.out.println(a/b);
}
catch{ (ArithmeticException e){
System.out.println("程序出现异常,变量b不能为0");
}
finally{//处理善后工作
System.out.println("finally");
}
//finally 可以不要finally,但是必须有try catch ,假设IO,资源,关闭!
public void a(){
b();
}
public void b(){
a();
}
try{
new text().a();
}
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")
}
try{//try监控区域
if(b==0){ // throw throws
throw new ArithmeticException();//主动的抛出异常
}
}
}
}
public class text1 {
public static void main(String [] args){
try{
new text1().text(1,0);
}
catch (AritmeticException e){
e.printStackTrace();
}
}
public void text(int a,int b){
if(b==0){
throw new ArithmeticException();//主动的抛出异常,一般在方法中实现
}
}
}