第一天
来公司实习实习培训的第一天
一、异常处理的方法
import java.util.Scanner;
/*
* 异常分为
* Error(虚拟机出现的问题不需要进行处理)
* Exception(运行时异常:除0、空指针、数组下标越界等
* 受查异常:日期格式转换等)
* */
public class Throw {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("请输入被除数:");
int a = sc.nextInt();
System.out.println("请输入除数:");
int b = sc.nextInt();
sc.close();
int[] arr = new int[1];
try {
//try是检测{}里面的代码有没有异常,所以将报异常的代码写在这里面
System.out.println("结果为:"+a/b);
arr[2] = 12;
} catch (ArithmeticException e) {
//多个catch的话catch里面的范围要从上往下变小
System.out.println("除数不能为0");
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组下标越界");
}finally{
//finally无论try是否检测出异常里面的语句都执行
System.out.println("我一直能执行!");
}
}
}
明天更新如何自己写异常!