文章目录
1. 异常概述
在程序中,错误可能产生于程序员没有预料到的各种情况,或者是因为超出了程序员控制之外的环境因素,如用户的坏数据、试图打开一个根本不存在的文件等。在Java中这种在程序运行时可能出现的一些错误称为异常。异常是一个在程序执行期间发生的事件,它中断了正在执行的程序的正常指令流。
例子:
public class Demo {
public static void main(String[] args) {
Object obj = null;
System.out.println(obj.hashCode());
}
}
结果如下:
系统报错,显示空指针异常。
例子:
public class Demo {
public static void main(String[] args) {
int arr[] = new int[6];
System.out.println(arr[10]);
}
}
结果,系统抛出数组下标越界的异常:
2. 处理程序异常错误
2.1 错误
异常产生后,如果不做任何处理,程序就会被终止。例如,将一个字符串转换为整型,可以通过Integer类的parseInt()方法来实现。但如果该字符串不是数字形式,parseInt()方法就会抛出异常,程序将停留在出现异常的位置,不再执行下面的语句。
例子:
public class Demo {
public static void main(String[] args) {
int a = 1/1 // 故意漏打一个;
System.out.println(a);
}
}
结果如下:
2.2 捕捉异常
Java语言的异常捕获结构由try、catch和finally 3部分组成。其中,try语句块存放的是可能发生异常的Java语句;catch程序块在try语句块之后,用来激发被捕获的异常;finally语句块是异常处理结构的最后执行部分,无论try块中的代码如何退出,都将执行finally块。
语法如下:
try {
// 程序代码块
}
catch(Exceptiontype1 e) {
// 对Exceptiontype1的处理
}
catch(Exceptiontype2 e) {
// 对Exceptiontype2的处理
}
...
finally {
// 程序块
}
2.2.1 try-catch 语句块
public class Demo {
public static void main(String[] args) {
try {
int a = 1/0;
}catch(ArithmeticException e) {
System.out.println("发生了算数异常,请及时处理!");
}
}
}
运行结果:
发生了算数异常,请及时处理!
Exception
是try
代码块传递给catch
代码块的变量类型,e是变量名。catch代码块语句中e.getMessage()
用于输出错误性质。通常,异常处理用以下3个函数来获取异常的有关信息。
- getMessage()函数:输出错误性质。
- toString()函数:给出异常的类型与性质。
- printStackTrace()函数:指出异常的类型、性质、栈层次及出现在程序中位置。
例子:
public class Demo {
public static void main(String[] args) {
try {
System.out.println("输出第1行");
int a = 1/0;
System.out.println("输出第2行");
System.out.println("输出第3行");
}catch(Exception e) {
e.printStackTrace();
}
}
}
结果如下:
如果在循环中出现异常,是否会中断循环?
public class Demo {
public static void main(String[] args) {
try {
for (int i = 0; i < 5; i++) {
System.out.println("输出第" + i + "行");
int a = 1 / i;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
结果如下:
如何在发生异常的情况下,让循环继续呢?
public class Demo {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
try {
System.out.println("输出第" + i + "行");
int a = 1 / i;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
得到结果:
2.2.2 finally 语句块
完整的异常处理语句一定要包含finally语句,无论程序找那个有无异常发生,并且无论之间的try-catch
是否顺利执行完毕,都会执行finally语句。
如果有异常:
如果无异常:
例子:
public class Demo {
public static void main(String[] args) {
try {
System.out.println("打开连接池");
int a = 1 / 0;
System.out.println("通过连接池读取数据");
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("关闭连接池");
}
}
}
结果如下:
以下4种特殊情况下,finally块不会被执行:
- 在finally语句块中发生了异常。
- 在前面的代码中使用了
System.exit()
退出程序。 - 程序所在的线程死亡。
- 关闭CPU。
3. Java 常见异常
4. 自定义异常
在程序中使用自定义异常类,大体可分为以下几个步骤:
- 创建自定义异常类。
- 在方法中通过throw关键字抛出异常对象。
- 如果在当前抛出异常的方法中处理异常,可以使用try-catch语句捕获并处理,否则在方法的声明处通过throws关键字指明要抛出给方法调用者的异常,继续进行下一步操作。
- 在出现异常方法的调用者中捕获并处理异常。
例子:
首先,创建NonHumansException类:
public class NonHumansException extends Exception {
public NonHumansException(String message) {
super(message);
}
}
然后,在Demo中输入代码:
public class Demo {
public static void main(String[] args) {
String playerType = "monkey";
try {
if (!playerType.equals("human")) {
throw new NonHumansException("有非人类选手" + playerType);
}
System.out.println("开始比赛");
} catch (NonHumansException e) {
e.printStackTrace();
}
}
}
运行,得到结果:
下面,我们将NonHumansException类进行修改:
import javax.swing.JOptionPane;
public class NonHumansException extends Exception {
String message;
public NonHumansException(String message) {
super(message);
this.message = message;
}
@Override
public void printStackTrace() {
super.printStackTrace();
JOptionPane.showMessageDialog(null, message, "发生异常", JOptionPane.ERROR_MESSAGE);
}
}
得到结果:
5. 在方法中抛出异常
5.1 使用throws关键字抛出异常
throws
关键字通常被应用在声明方法时,用来指定方法可能抛出的异常。多个异常可使用逗号分隔。
public class Test {
public static void show() throws InterruptedException {
for (int i = 0; i < 10; i++) {
System.out.println(i);
Thread.sleep(1000); // 休眠1秒
}
}
public static void main(String[] args) {
try {
show(); // 此处调用show()方法时,系统会报错,要求我们捕获异常
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
此外,还可以在show()方法中添加其他的异常:
添加完空指针等异常后,系统会提示我们增加捕获其他异常,选中add catch,系统会自动添加:
public class Test {
public static void show() throws InterruptedException,NullPointerException,Exception {
for (int i = 0; i < 10; i++) {
System.out.println(i);
Thread.sleep(1000); // 休眠1秒
}
}
public static void main(String[] args) {
try {
show(); // 此处调用show()方法时,系统会报错,要求我们捕获异常
} catch (InterruptedException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用throws
关键字将异常抛给上一级后,如果不想处理该异常,可以继续向上抛,但最终要有能够处理该异常的代码。
如果是Error、RuntimeException或它们的子类,可以不使用throws关键字来声明要抛出的异常,编译仍能顺利通过,但在运行时会被系统抛出。
5.2 使用throw关键字抛出异常
throw关键字通常用于方法体中,并且抛出一个异常对象。程序在执行到throw
语句时立即终止,它后面的语句都不执行。通过throw
抛出异常后,如果想在上一级代码中来捕获并处理异常,则需要在抛出异常的方法中使用throws
关键字在方法的声明中指明要抛出的异常;如果要捕捉throw
抛出的异常,则必须使用try-catch
语句。
throw
通常用来抛出用户自己定义异常。
例子:
public class Test {
public static void main(String[] args) {
int count = -100;
if (count < 0) {
throw new ArithmeticException("人员数量是负数:" + count);
}
System.out.println("当前统计的人数为:" + count);
}
}
运行结果:
这里是我们自己定义的一个异常,完整的写法应该是这样的:
public class Test {
public static void main(String[] args) {
try {
int count = -100;
if (count < 0) {
throw new ArithmeticException("人员数量是负数:" + count);
}
System.out.println("当前统计的人数为:" + count);
} catch (Exception e) {
e.printStackTrace();
System.out.println("捕捉到异常");// 手动添加捕捉异常输出
}
}
}
运行结果:
下面我们改写一下:
public class Test {
public static void main(String[] args) {
try {
int a = 1, b = 0;
if (b == 0) {
throw new NullPointerException("b等于0,发生异常");// 定义一个空指针异常
}
int c = a / b;// 如果运行到这里,会抛出算数异常
} catch (Exception e) {
e.printStackTrace();
System.out.println("捕捉到异常");// 手动添加捕捉异常输出
}
}
}
运行结果:
6. 运行时异常
RuntimeException
异常是程序运行过程中产生的异常。Java类库中的每个包中都定义了异常类,所有这些类都是Throwable
类的子类。Throwable
类派生了两个子类,分别是Exception
和Error
类。Error
类及其子类用来描述Java运行系统中的内部错误以及资源耗尽的错误,这类错误比较严重。Exception
类称为非致命性类,可以通过捕捉处理使程序继续执行。Exception
类又根据错误发生的原因分为RuntimeException
异常和RuntimeException
之外的异常。
7. 异常的使用原则
在程序中使用异常,可以捕获程序中的错误,但是异常的使用也要遵循一定的规则,下面是异常类的几项使用原则。
- 不要过多的使用异常,这样会增加系统的负担。
- 在方法中使用try-catch捕获异常时,要对异常作出处理。
- try-catch语句块的范围不要太大,这样不利于对异常的分析。
- 一个方法被覆盖时,覆盖它的方法必须抛出相同的异常或子异常。(运行时异常RuntimeException不受此约束)