标签:使用 exception 处理 系统 程序 ble scanner size stack
Throwable是异常的基类
异常处理(try使用):
public class Demo {
public static void main(String[] args){
test();
}
public static void test(){
int[] arr = {1,2,3};
try{
System.out.print(arr[8]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println(e);
}
catch(Exception e){
e.printStackTrace(); //可以打印栈错误信息
System.out.println("错误");
}finally{
System.out.println("无论是否产生异常,都要执行");//即使系统报错,程序会异常退出,也会执行,即使try中有return,finally也会在return之前执行
}
}
}
throws和Throw使用:
public class Demo {
public static void main(String[] args){
test();
}
public static int test() throws ArrayIndexOutOfBoundsException{
Scanner s = new Scanner(System.in);
try{
int[] arr = {1,2};
int x = arr[4];
return 1;
}catch(ArrayIndexOutOfBoundsException e){
// e.printStackTrace();
throw new ArrayIndexOutOfBoundsException("出错了");//主动抛出错误
// return 1;
}
}
}
自定义错误:
标签:使用 exception 处理 系统 程序 ble scanner size stack
原文地址:https://www.cnblogs.com/yanxiaoge/p/10682636.html