throws和try catch的区别、常见异常类
简单说
一、异常的产生:
1、系统自动生成的异常对象
2、throw new Exception()
手动抛出异常,即生成一个异常对象抛出
二、异常的解决:
1、try catch finally
当try中代码出现异常,会生成对应的异常类对象,如果该对象与catch捕获类型列表对应,会进入对应catch代码,捕获并处理异常,不用由上级帮忙处理,是真正解决了异常
异常代码后的代码,仍会执行!
2、throws Exception 声明异常
表示被声明代码有可能抛出异常
当代码异常,仍会在代码异常处生成异常对象,如果该对象与throws 抛出的异常类型对应,就会抛出给方法调用处,去求助别人
异常代码后的代码,就不再执行!
面试题:常见的异常类有哪些?
编译时异常:
IOException
-----FileNotFoundException
ClassNotFoundException
运行时异常(RuntimeException):
NullPointerException, int []arr=null;输出a[1]
ArrayIndexOutOfBoundsException,int a[]=new int [5]; 输出a[5]
ArithmeticException(/0),
ClassCastException,: Object o= new Date(); String str=(String) o;
illegalClassFormatException
InputMismatchException : int a= new Scanner(System.in).nextInt() 却输入一个字符串
NumberFormatException :Integer.parseInt("abc")

浙公网安备 33010602011771号