异常处理总结
1.异常概述
1.1 异常分类
- Error:Java虚拟机无法解决的严重问题,一般不编写针对性的代码进行处理(需要直接对源代码进行修改)。比如:StackOverflowError 和 OOM
- Exception: 其它因编程错误或偶然的外在因素导致的一般性问题,可以使用针对性的代码进行处理
1.2 异常体系结构
- checked异常:即编译时异常。编译器要求Java程序必须捕获或声明所有编译时异常。
- unchecked异常:即运行时异常。编译器不要求强制处置的异常。是程序员应该积极避免其出现的异常java.lang.RuntimeException类及其子类都是运行时异常。对于这类异常可以不作处理,因为这类异常很普遍,若全处理可能会对程序的可读性和运行效率产生影响。
2.异常处理机制——try-catch-finally
2.1抓抛模型
(1) “抛”:程序在正常执行的过程中,一旦出现异常,就会在异常代码处生成一个对应异常类的对象,并将此对象抛出,且其后的代码不再执行。
(2) “抓”:即异常的处理方式
① try-catch-finally
② throws
(3) 异常对象的产生
① 系统自动生成的异常对象
② 手动的生成一个异常对象并抛出(throw)
2.2使用格式
try
{
可能会出现异常的代码
}
catch(异常类型1 变量名1)
{
处理异常的方式1
}
catch(异常类型2 变量名2)
{
处理异常的方式2
}
...
finally
{
一定会执行的代码
}
2.3使用说明
- 使用try将可能出现异常代码包装起来,在执行过程中一旦出现异常,就会生成一个对应异常类的对象,根据此对象的类型去catch中进行匹配。一旦try中的异常对象匹配成功,就进入catch中进行异常的处理。(不再继续匹配后面的catch结构)一旦处理完成,就跳出当前的 try-catch结构。(在没有写finally的情况下)继续执行后面的语句。
- catch中的异常类型如果满足子父类关系,则要求子类一定声明在父类的之前。
public static void main(String[] args)
{
String str="abc";
try
{
int num = Integer.parseInt(str);
} catch (NumberFormatException e)
{
System.out.println("出现数值转换异常了,不要着急....");
} catch (NullPointerException e)
{
System.out.println("出现空指针异常了,不要着急....");
} catch (Exception e)
{
System.out.println("出现异常了,不要着急....");
}
}
输出
//出现数值转换异常了,不要着急....
NumberFormatException是Exception的子类。不能颠倒顺序
匹配到 NumberFormatException 后不再继续匹配
- 常用的异常对象处理的方式:
① String getMessage()
② void printStackTrace() - finally是可选的。finally 内的语句一定会被执行。即使catch中又出现异常了、catch中有 return语句、try中有return语句 等情况。
public int method()
{
try
{
int[] arr = new int[10];
System.out.println(arr[10]);
return 1;
} catch (ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
return 2;
} finally
{
System.out.println("我一定会被执行");
}
}
System.out.println(method());
//输出
我一定会被执行
2
public int method()
{
try
{
int[] arr = new int[10];
System.out.println(arr[10]);
return 1;
} catch (ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
return 2;
} finally
{
System.out.println("我一定会被执行");
return 3;
}
}
System.out.println(method());
//输出
我一定会被执行
3
- 为何需要使用finally:数据库连接、输入输出流、网络编程Socket等资源,JVM是不能自动的回收的,我们需要自己手动的进行资源的释放。为了避免异常处理时跳过资源回收,此时需要将资源回收声明在finally中。
FileInputStream fis = null;
try
{
File file = new File("hello1.txt");
fis = new FileInputStream(file);
int data = fis.read();
while (data != -1)
{
System.out.print((char) data);
data = fis.read();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (fis != null)
fis.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
3.异常处理机制——throws
3.1使用格式
throws 异常类型1,异常类型2...
public int method( ) throws FileNotFoundException,IOException
3.2使用说明
- "throws + 异常类型"写在方法的声明处。指明此方法执行时,可能会抛出的异常类型。一旦当方法体执行时出现异常,会在异常代码处生成一个异常类的对象,若此对象满足throws异常类型时就会被抛出。异常代码后续的代码不再执行。
3.3 try-catch-finally 和throws的区别
try-catch-finally: 将异常真正的处理了。
throws:将异常抛给了方法的调用者。 并没有真正将异常处理。
3.4 try-catch-finally 和throws的选择
- 因为子类重写方法抛出的异常类型不能大于父类抛出的异常类型。如果父类中被重写的方法没有throws方式处理异常,则子类重写的方法也不能使用throws。则如果子类重写的方法中有异常,必须使用try-catch-finally方式处理。
- 假设方法 A调B B调C C调D 。建议 BCD 方法使用throws进行异常处理。A使用try-catch-finally方式进行处理。
4.手动抛出异常
4.1 使用格式
// 先new对象才能抛出
1.throw new FileNotFoundException;
2.FileNotFoundException e=new FileNotFoundException();
throw e;
4.1 使用说明
- 抛出的异常必须是 Throwable 或其子类的实例
class Student{
private int id;
public void regist(int id)
{
if(id > 0)
{
this.id = id;
}
else
{
throw new RuntimeException("您输入的数据非法!");
//错误的 throw new MyException("不能输入负数");
}
}
}
5.自定义异常
- 用户自定义异常类必须继承于现有异常类。
- 自定义异常类通常需要编写几个重载的构造器。
- 自定义异常需要提供 serialVersionUID
- 自定义的异常通过throw抛出。
public class MyException extends RuntimeException
{
static final long serialVersionUID = -7034897193246939L;
public MyException() { }
public MyException(String msg)
{
super(msg);
}
}