s-day05--try..catch处理多个异常
This commit is contained in:
@@ -0,0 +1,46 @@
|
|||||||
|
package com.inmind.exception01;
|
||||||
|
/*
|
||||||
|
try..catch处理多个异常
|
||||||
|
|
||||||
|
在实际开发中,一段业务代码,可能会出现多种不同的异常,所以我们要进行多异常处理
|
||||||
|
|
||||||
|
try...catch的语法:
|
||||||
|
try{
|
||||||
|
可能会出现异常的代码或者方法
|
||||||
|
}catch(指定异常类名 变量名){
|
||||||
|
捕获异常后,异常处理代码
|
||||||
|
}catch(指定异常类名 变量名){
|
||||||
|
捕获异常后,异常处理代码
|
||||||
|
}catch(指定异常类名 变量名){
|
||||||
|
捕获异常后,异常处理代码
|
||||||
|
}.....
|
||||||
|
|
||||||
|
注意:在实际开发中,具体的异常处理,都是要进行相关的业务操作,而不是固定操作
|
||||||
|
*/
|
||||||
|
public class Demo09 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
System.out.println("try开始了------【1】");
|
||||||
|
int i = 2;
|
||||||
|
if (i < 6) {
|
||||||
|
// throw new NullPointerException();
|
||||||
|
throw new ClassCastException();
|
||||||
|
}
|
||||||
|
if (i >= 6) {
|
||||||
|
throw new ArrayIndexOutOfBoundsException();
|
||||||
|
}
|
||||||
|
System.out.println("try结束了------【2】");
|
||||||
|
} catch (NullPointerException e) {
|
||||||
|
System.out.println("处理了空指针异常");
|
||||||
|
} catch (ArrayIndexOutOfBoundsException e) {
|
||||||
|
System.out.println("处理了越界异常");
|
||||||
|
} catch (Exception e){//托底操作
|
||||||
|
System.out.println("除了空和越界以外的异常,都这么处理");
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
System.out.println("finally");
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user