目录
7.1 异常概述与异常体系结构
在使用计算机语言进行项目开发的过程中,即使程序员把代码写得尽善尽美,
在系统的运行过程中仍然会遇到一些问题,因为很多问题不是靠代码能够避
免的,比如:
客户输入数据的格式,读取文件是否存在,网络是否始终保持
通畅
等等。
1 异常介绍
异常:在Java语言中,将程序执行中发生的不正常情况称为“异常”。
(开发过程中的语法错误和逻辑错误不是异常)
异常可分为两类:
-
Error:Java虚拟机无法解决的严重问题。如:JVM系统内部错误、资源 耗尽等严重情况。比如: StackOverflowError 和 OOM 。一般不编写针对性 的代码进行处理。
-
Exception: 其它因编程错误或偶然的外在因素导致的一般性问题,可以使 用针对性的代码进行处理。例如:
-
空指针访问
-
试图读取不存在的文件
-
网络连接中断
-
数组角标越界
2 异常体系结构

7.2 常见异常
//运行时异常
//NullPointerException
int[] arr = null;
System.out.println(arr[3]);
//ArithmeticException
int a = 10;
int b = 0;
System.out.println(a/b);
//ArrayIndexOutOfBoundsException
int[] arr = new int[10];
System.out.println(arr[10]);
//ClassCastException
Object obj = new Date();
String s = (String)obj;
//NumberFormatException
String sre = "abc";
int num = Integer.parseInt(str);
//InputMismatchException
Scanner sc = new Scanner(System.in);
int score = scanner.nextInt();
System.out.println(score);
//编译时异常
public void test1() {
File file = new File("hello.txt");
FileInputStream fis = new FileInputStream(file); //FileNotFoundException
int data = fis.read(); //IOException
while(data != -1) {
System.out.println((char)data);
data = fis.read(); //IOException
}
fis.close(); //IOException
}
7.3 异常处理机制一:try-catch-finally
在编写程序时,经常要在可能出现错误的地方加上检测的代码,
如进行x/y运算时,要检测分母为0,数据为空,输入的不是数据
而是字符等。过多的if-else分支会导致程序的代码加长、臃肿,
可读性差。因此采用异常处理机制。
Java异常处理
Java采用的异常处理机制,是将异常处理的程序代码集中在一起,
与正常的程序代码分开,使得程序简洁、优雅,并易于维护。
1 异常的处理:抓抛模型
过程一:“抛”
程序在执行过程中,一旦出现异常,就会在异常代码处生成一个对应异常类的对象。并将此对象抛出。一旦抛出以后,其后的代码不再执行。
异常对象的产生:①系统自动生成的异常对象
②手动生成异常对象,并抛出(throws)。
过程二:“抓”
可以理解为异常的处理方式:①try-catch-finally ②throws
2 try-catch-finally的使用
格式:
try{
//可能出现异常的代码
}catch(异常类型1 变量名1){
//处理异常的方式1
}catch(异常类型2 变量名2){
//处理异常的方式2
}
......
finally{
//一定会执行的代码
}
说明:
-
使用try将可能出现的异常代码包装起来,在执行过程中,一旦出现异常,就会 生成一个对应异常类的对象,根据此对象的类型,去catch中匹配。
-
一但try中的异常对象匹配到某一个catch时,进入该catch进行处理。一旦处理完成就跳出当前try-catch结构,继续执行后面的代码。
-
catch中的异常如果满足子父类关系,则要求子类一定要声明在父类上面。
-
try结构中声明的变量,出了try结构以后不能再调用。
//catch中几种常用处理方式
System.out.println(e.getMessage()); //获取异常信息
printStackTrace(); //获取堆栈信息
注:
-
使用try-catch-finally处理编译时异常,使得程序在编译时不再报错,运行时仍可能报错。相当于将编译异常延迟到运行时出现。
-
开发中,运行时异常比较常见,所以我们通常不针对运行时异常编写try-catch-finally。
finally使用
-
finally是可选的。
-
finally中声明的是一定会被执行的代码,即使catch中出现异常,或者try catch中有return语句。
-
什么代码需要被写在finally中? 数据库连接,输入输出流,网络编程中的socket等资源,JVM是不能自动回收的,需要自己手动进行资源释放,此时的释放代码就需要写在finally中。
-
try-catch-finally可以相互嵌套。
public void test1() {
FileInputStream fis = null;
try {
File file = new File("hello.txt");
fis = new FileInputStream(file); //FileNotFoundException
int data = fis.read(); //IOException
while(data != -1) {
System.out.println((char)data);
data = fis.read(); //IOException
}
}catch(FileNotFoundException e){
System.out.println(e.getMessage());
}catch(IOException e) {
System.out.println(e.getMessage());
}finally {
try {
if(fis != null)
//资源释放需要写在finally中,由于这句话本身也报IOException错误,还需要嵌套一层try-catch
fis.close(); //IOException
} catch (IOException e) {
e.printStackTrace();
}
}
}
7.4 异常处理机制二:throws
1 throws
写在方法声明处,指明执行时可能会抛出的异常。一旦出现异常,仍会在异常代码处生成一个异常类对象,此对象满足throws后异常类型时,就会被抛出。异常代码后续的代码就不再执行。
try-catch-finally:真正的处理掉异常了
throws:只是将异常抛给了调用者,并没有真正处理掉
public void method(){
try {
test1();
} catch (IOException e) {
e.printStackTrace();
}
}
public void test1() throws FileNotFoundException, IOException{
File file = new File("hello.txt");
FileInputStream fis = new FileInputStream(file); //FileNotFoundException
int data = fis.read(); //IOException
while(data != -1) {
System.out.println((char)data);
data = fis.read(); //IOException
}
fis.close(); //IOException
}
2 方法重写的规则之一
子类重写方法抛出的异常不大于父类被重写方法抛出的异常
public class OverrideTest {
public static void main(String[] args) {
OverrideTest t = new OverrideTest();
t.display(new SubClass());
}
public void display(SuperClass s) {
try {
s.method();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class SuperClass{
public void method() throws IOException{
}
}
class SubClass extends SuperClass{
public void method() throws FileNotFoundException{
}
}
3 开发中如何选择异常处理方式?
-
如果父类中被重写的方法没有throws抛出异常,意味着子类方法如果有异常,则必须使用try-catch-finally。
-
执行的方法a中,先后调用了另外的几个方法,并且是递进的。我们建议几个方法使用throws的方法处理,而执行的方法a中可以考虑使用try-catch-finally方式进行处理。
7.5 手动抛出异常
异常对象的产生:①系统自动生成的异常对象
②手动生成异常对象,并抛出(throws)。
//方式一:抛出运行时异常,不处理
class Student{
private int id;
public void regist(int id) {
//id必须要大于0
if(id > 0)
this.id = id;
else {
//需要手动抛出异常对象
throw new RuntimeException("非法数据");
}
}
}
//方式二:抛出编译时异常,在调用该方法时处理
class Student{
private int id;
public void regist(int id) throws Exception {
//id必须要大于0
if(id > 0)
this.id = id;
else {
//需要手动抛出异常对象
throw new Exception("非法数据");
}
}
}
7.6 用户自定义异常类
1 如何自定义异常类
/**
* 1.继承于现有的异常结构:Exception、RuntimeException
* 2.提供全局常量:serialVersionUID(类的标识)
* 3.提供重载构造器
*
*/
public class MyException extends Exception{
static final long serialVersionUID = -7034897190745766939L;
public MyException() {
}
public MyException(String msg) {
super(msg);
}
}
//使用
class Student{
private int id;
public void regist(int id) throws Exception {
//id必须要大于0
if(id > 0)
this.id = id;
else {
//需要手动抛出异常对象
throw new myException("非法数据");
}
}
}
总结
