一、什么是异常
导致程序的正常流程被中断的事件,叫做异常
(1) 可查异常: CheckedException,可查异常即必须进行处理的异常,要么try catch住,要么往外抛,谁调用,谁处理。
(2)运行时异常:RuntimeException指: 不是必须进行try catch的异常。
(3)错误Error,指的是系统级别的异常,通常是内存用光了。在默认设置下,一般java程序启动的时候,最大可以使用16m的内存。与运行时异常一样,错误也是不要求强制捕捉的。
(4)Throwable是类,Exception和Error都继承了该类,所以在捕捉的时候,也可以使用Throwable进行捕捉。
补充:
ArithmeticException——由于除数为0引起的异常;
ArrayStoreException——由于数组存储空间不够引起的异常;
ClassCastException——当把一个对象归为某个类,但实际上此对象并不是由这个类创建的,也不是其子类创建的,则会引起异常;
IllegalMonitorStateException——监控器状态出错引起的异常;
NegativeArraySizeException——数组长度是负数,则产生异常;
NullPointerException——程序试图访问一个空的数组中的元素或访问空的对象中的方法或变量时产生异常;
OutofMemoryException——用new语句创建对象时,如系统无法为其分配内存空间则产生异常;
SecurityException——由于访问了不应访问的指针,使安全性出问题而引起异常;
IndexOutOfBoundsExcention——由于数组下标越界或字符串访问越界引起异常;
IOException——由于文件未找到、未打开或者I/O操作不能进行而引起异常;
ClassNotFoundException——未找到指定名字的类或接口引起异常;
CloneNotSupportedException——程序中的一个对象引用Object类的clone方法,但此对象并没有连接Cloneable接口,从而引起异常;
InterruptedException——当一个线程处于等待状态时,另一个线程中断此线程,从而引起异常;
NoSuchMethodException——所调用的方法未找到,引起异常;
Illega1AccessExcePtion——试图访问一个非public方法;
StringIndexOutOfBoundsException——访问字符串序号越界,引起异常;
ArrayIdexOutOfBoundsException——访问数组元素下标越界,引起异常;
NumberFormatException——字符的UTF代码数据格式有错引起异常;
IllegalThreadException——线程调用某个方法而所处状态不适当,引起异常;
FileNotFoundException——未找到指定文件引起异常;
EOFException——未完成输入操作即遇文件结束引起异常。
二、异常处理
1.try catch
public class TestException {
public static void main(String[] args) {
File f= new File("d:/LOL.exe");
try{
System.out.println("试图打开 d:/LOL.exe");
new FileInputStream(f);
System.out.println("成功打开");
}
catch(FileNotFoundException e){
System.out.println("d:/LOL.exe不存在");
e.printStackTrace();
}
}
}
2.多异常捕捉办法1
使用多个catch
public class TestException {
public static void main(String[] args) {
File f = new File("d:/LOL.exe");
try {
System.out.println("试图打开 d:/LOL.exe");
new FileInputStream(f);
System.out.println("成功打开");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse("2016-06-03");
} catch (FileNotFoundException e) {
System.out.println("d:/LOL.exe不存在");
e.printStackTrace();
} catch (ParseException e) {
System.out.println("日期格式解析错误");
e.printStackTrace();
}
}
}
3.多异常捕捉办法2
使用一个catch
public class TestException {
public static void main(String[] args) {
File f = new File("d:/LOL.exe");
try {
System.out.println("试图打开 d:/LOL.exe");
new FileInputStream(f);
System.out.println("成功打开");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse("2016-06-03");
} catch (FileNotFoundException | ParseException e) {
if (e instanceof FileNotFoundException)
System.out.println("d:/LOL.exe不存在");
if (e instanceof ParseException)
System.out.println("日期格式解析错误");
e.printStackTrace();
}
}
}
4.finally
无论是否出现异常,finally中的代码都会被执行。
public class TestException {
public static void main(String[] args) {
File f= new File("d:/LOL.exe");
try{
System.out.println("试图打开 d:/LOL.exe");
new FileInputStream(f);
System.out.println("成功打开");
}
catch(FileNotFoundException e){
System.out.println("d:/LOL.exe不存在");
e.printStackTrace();
}
finally{
System.out.println("无论文件是否存在, 都会执行的代码");
}
}
}
5.throws
如果方法中中需要进行异常处理,但是该方法不打算处理,而是把这个异常通过throws抛出去,那么调用该方法的方法就会接住该方法就会接住该异常,如果该异常被catch住了,就会被消化掉。很绕是吧,看图。
public class TestException {
public static void main(String[] args) {
method1();
}
private static void method1() {
try {
method2();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private static void method2() throws FileNotFoundException {
File f = new File("d:/LOL.exe");
System.out.println("试图打开 d:/LOL.exe");
new FileInputStream(f);
System.out.println("成功打开");
}
}
6.throw和throws的区别
(1) throws 出现在方法声明上,而throw通常都出现在方法体内。
(2)throws 表示出现异常的一种可能性,并不一定会发生这些异常;throw则是抛出了异常,执行throw则一定抛出了某个异常对象。