1.try-catch
try{
//一些会抛出异常的方法
}catch(Exception){
//处理该异常的代码块
}

2.多个catch

编写多个catch语句应注意:先小后大,也就是先子类后父类
try{
//一些会抛出异常的方法
}catch(Exception e){
//处理该异常的代码块
}catch(Exception2 e){
//处理该异常的代码块
}…(n个catch块)…{
//处理该异常的代码块
}
错误书写:

3.try-catch-finally
try{
//一些会抛出异常的方法
}catch(Exception e){
//处理该异常的代码块
}catch(Exception2 e){
//处理该异常的代码块
}…(n个catch块)…{
//处理该异常的代码块
}finally{
//最终将要执行的一些代码
}
网友评论