异常就是我们常说的程序bug,指程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的。
比如说,你的代码少了一个分号,那么运行出来结果是提示是错误 java.lang.Error;如果你用System.out.println(66/0),那么你是因为你用0做了除数,会抛出 java.lang.ArithmeticException 异常。
异常发生的原因有很多,通常包含以下几大类:
- 用户输入了非法数据。
- 要打开的文件不存在。
- 网络通信时连接中断,或者JVM内存溢出。
这些异常有的是因为用户错误引起,有的是程序错误引起的,还有其它一些是因为物理错误引起的。
异常一般指Exception及其子类,但是广义上的异常是包括Exception和Error;
所有的异常类是从 java.lang.Exception 类继承的子类。
Exception 类是 Throwable 类的子类。除了Exception类外,Throwable还有一个子类Error 。
Java 程序通常不捕获错误。错误一般发生在严重故障时,它们在Java程序处理的范畴之外。
Error 用来指示运行时环境发生的错误。
例如,JVM 内存溢出。一般地,程序不会从错误中恢复。
异常类有两个主要的子类:IOException 类和 RuntimeException 类。
我们看看下面这张图,具体介绍三种异常类型:
异常需要掌握以下三种类型:
- 检查性异常:同非运行时异常,最具代表的检查性异常是用户错误或问题引起的异常,这是程序员无法预见的。例如要打开一个不存在文件时,一个异常就发生了,这些异常在编译时不能被简单地忽略。也就是说,代码还未运行,编译器就会检查你的代码,对可能出现的异常要求必须做出相应处理,否则编译失败。
- 运行时异常: 同非检查性异常,运行时异常是可以被避免的异常,写对就行,如果有异常抛出直接抛到控制台。与检查性异常相反,运行时异常可以在编译时被忽略,运行时异常的特点是Java编译器不会检查它,当程序中可能出现这类异常,即使没有用try-catch语句捕获它,也没有用throws子句声明抛出,也会编译通过。
- 错误: 错误不是异常,而是脱离程序员控制的问题。错误在代码中通常被忽略。例如,当栈溢出时,一个错误就发生了,它们在编译也检查不到的。
//Exception类的完整源代码:继承了java.lang.Throwable
public class Exception extends Throwable {
//适用于java序列化机制,过判断类的serialVersionUID来验证的版本一致的
static final long serialVersionUID = -3387516993124229948L;
//构造一个异常的详细信息,可通过调用initCause来初始化,默认值为null
public Error() {
super();
}
//使用指定的详细信息消息构造新异常。
public Error(String message) {
super(message);
}
//使用指定的详细信息和原因构造新异常。
public Error(String message, Throwable cause) {
super(message, cause);
}
//使用指定的原因构造新异常。
public Error(Throwable cause) {
super(cause);
}
//使用指定的详细信息消息、原因、启用或禁用禁止显示以及启用或禁用可写堆栈跟踪来构造新异常。
protected Error(String message, Throwable cause,
boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
其上的cause成员变量是用来存贮引发某个异常或错误的异常或错误类的,也就是说它保存着引发异常或错误的原因异常类,如果没有赋值的话,默认是其自身,默认没有造成当前异常的异常类,也就是getCause方法等于默认的时候,返回的是null的原因,而且getCause是一个同步方法,保证了线程的安全,因为异常或错误信息的返回是有一个延时性的,防止了返回信息的时候遭到了其他子线程的修改。这也形成了一个因果关系,并且可以形成一条因果异常链,即我的throable是你引起的,而你的thowable是他引起的,这就构成了一条因果链了。
非检查性异常 | 描述 |
ArithmeticException | 当出现异常的运算条件时,抛出此异常。例如,一个整数"除以零"时,抛出此类的一个实例 |
ArrayIndexOutOfBoundsException | 用非法索引访问数组时抛出的异常。如果索引为负或大于等于数组大小,则该索引为非法索引 |
ArrayStoreException | 试图将错误类型的对象存储到一个对象数组时抛出的异常 |
ClassCastException | 当试图将对象强制转换为不是实例的子类时,抛出该异常 |
IllegalArgumentException | 抛出的异常表明向方法传递了一个不合法或不正确的参数 |
IllegalMonitorStateException | 抛出的异常表明某一线程已经试图等待对象的监视器,或者试图通知其他正在等待对象的监视器而本身没有指定监视器的线程 |
IllegalStateException | 在非法或不适当的时间调用方法时产生的信号。换句话说,即 Java 环境或 Java 应用程序没有处于请求操作所要求的适当状态下 |
IllegalThreadStateException | 线程没有处于请求操作所要求的适当状态时抛出的异常 |
IndexOutOfBoundsException | 指示某排序索引(例如对数组、字符串或向量的排序)超出范围时抛出 |
NegativeArraySizeException | 如果应用程序试图创建大小为负的数组,则抛出该异常 |
NullPointerException | 当应用程序试图在需要对象的地方使用 null 时,抛出该异常 |
NumberFormatException | 当应用程序试图将字符串转换成一种数值类型,但该字符串不能转换为适当格式时,抛出该异常 |
SecurityException | 由安全管理器抛出的异常,指示存在安全侵犯 |
StringIndexOutOfBoundsException | 此异常由 String 方法抛出,指示索引或者为负,或者超出字符串的大小 |
UnsupportedOperationException | 当不支持请求的操作时,抛出该异常 |
检查性异常 | 描述 |
ClassNotFoundException | 应用程序试图加载类时,找不到相应的类,抛出该异常 |
CloneNotSupportedException | 当调用 Object 类中的 clone 方法克隆对象,但该对象的类无法实现 Cloneable 接口时,抛出该异常 |
IllegalAccessException | 拒绝访问一个类的时候,抛出该异常 |
InstantiationException | 当试图使用 Class 类中的 newInstance 方法创建一个类的实例,而指定的类对象因为是一个接口或是一个抽象类而无法实例化时,抛出该异常 |
InterruptedException | 一个线程被另一个线程中断,抛出该异常 |
NoSuchFieldException | 请求的变量不存在 |
NoSuchMethodException | 请求的方法不存在 |
大多异常用不太到,了解即可,后面会罗列较为常用异常的实例。
异常方法 | 方法及说明 |
1 | public String getMessage() 返回关于发生的异常的详细信息。这个消息在Throwable 类的构造函数中初始化了 |
2 | public Throwable getCause() 返回一个 Throwable 对象代表异常原因 |
3 | public String toString() 返回此 Throwable 的简短描述 |
4 | public void printStackTrace() 将此 Throwable 及其回溯打印到标准错误流,获取异常类名和异常信息,以及异常出现在程序中的位置,返回值void |
5 | public StackTraceElement [] getStackTrace() 调用堆栈的栈底 |
6 | public Throwable fillInStackTrace() 用当前的调用栈层次填充Throwable 对象栈层次,添加到栈层次任何先前信息中 |
使用 try 和 catch 关键字可以捕获异常。try/catch 代码块放在异常可能发生的地方。
try/catch代码块中的代码称为保护代码,使用 try/catch 的语法如下:
try { // 程序代码 }catch(ExceptionName e1) { //Catch 块 }/*catch 语句包含要捕获异常类型的声明。当保护代码块中发生一个异常时,try 后面的 catch 块就会被检查。如果发生的异常包含在 catch 块中,异常会被传递到该 catch 块,这和传递一个参数到方法是一样。*/
// 数组下标越界
import java.io.*;
public class ExcepTest{
public static void main(String args[]){
try{
int a[] = new int[2];
System.out.println("Access element three :" + a[3]);//本来就0、1、2,无3
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Exception thrown :" + e);
}
System.out.println("Out of the block");
}
}
/*
Exception thrown :java.lang.ArrayIndexOutOfBoundsException: 3
Out of the block
*/
try{
// 程序代码
}catch(异常类型1 异常的变量名1){
// 程序代码
}catch(异常类型2 异常的变量名2){
// 程序代码
}catch(异常类型3 异常的变量名3){
// 程序代码
}/*
上面的代码段包含了 3 个 catch块。
可以在 try 语句后面添加任意数量的 catch 块。
如果保护代码中发生异常,异常被抛给第一个 catch 块。
如果抛出异常的数据类型与异常类型1匹配,它在其中就会被捕获。
如果不匹配,它会被传递给第二个 catch 块。
如此,直到异常被捕获或者通过所有的 catch 块。
*/
try {
file = new FileInputStream(fileName);
x = (byte) file.read();
} catch(FileNotFoundException f) { // Not valid!
f.printStackTrace();
return -1;
} catch(IOException i) {
i.printStackTrace();
return -1;
}
JDK7 之后,Java 新增的 try-with-resource 语法糖来打开资源,并且可以在语句执行完毕后确保每个资源都被自动关闭 ,它可以简化资源管理代码的编写,所有被打开的系统资源,比如流、文件或者 Socket 连接等,都需要close(),否则将会造成资源泄露。
try (resource declaration) {
// 使用的资源
} catch (ExceptionType e1) {
// 异常块
}// try 用于声明和实例化资源,catch 用于处理关闭资源时可能引发的所有异常。
注意:try-with-resources 语句关闭所有实现 AutoCloseable 接口的资源。
实例一个 BufferedReader 对象从 test.txt 文件中读取数据。
在 try-with-resources 语句中声明和实例化 BufferedReader 对象,执行完毕后自动关闭资源,不需要考虑 try 语句是正常执行还是抛出异常。
//1、使用try-with-resources
import java.io.*;
public class Test1 {
public static void main(String[] args) {
String line;
//try (resource declaration),这样打开使用完后会自动关闭,从而简化代码
try(BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
while ((line = br.readLine()) != null) {
System.out.println("Line =>"+line);
}
} catch (IOException e) {
System.out.println("IOException in try block =>" + e.getMessage());
}
}
}
//IOException in try block =>test.txt (No such file or directory)
//2、不使用 try-with-resources 而改成 finally 来关闭资源
import java.io.*;
class Test2 {
public static void main(String[] args) {
BufferedReader br = null;
String line;
try {
System.out.println("Entering try block");
br = new BufferedReader(new FileReader("test.txt"));
while ((line = br.readLine()) != null) {
System.out.println("Line =>"+line);
}
} catch (IOException e) {
System.out.println("IOException in try block =>" + e.getMessage());
} finally {
System.out.println("Entering finally block");
try {
//手动关闭
if (br != null) {
br.close();
}
} catch (IOException e) {
System.out.println("IOException in finally block =>"+e.getMessage());
}
}
}
}
/*
Entering try block
IOException in try block =>test.txt (No such file or directory)
Entering finally block
*/
/*try-with-resources 处理多个资源,方法是使用分号分隔各个资源:
使用 Scanner 对象从 testRead.txt 文件中读取一行并将其写入新的 testWrite.txt 文件中。
多个声明资源时,try-with-resources 语句以相反的顺序关闭这些资源。 在本例中,PrintWriter 对象先关闭,然后 Scanner 对象关闭。*/
import java.io.*;
import java.util.*;
class RunoobTest {
public static void main(String[] args) throws IOException{
try (Scanner scanner = new Scanner(new File("testRead.txt"));
PrintWriter writer = new PrintWriter(new File("testWrite.txt"))) {
while (scanner.hasNext()) {
writer.print(scanner.nextLine());
}
}
}
}
throw 关键字用于在当前方法中抛出一个异常。
通常情况下,当代码执行到某个条件下无法继续正常执行时,可以使用 throw 关键字抛出异常,以告知调用者当前代码的执行状态。
public void checkNumber(int num) {
if (num < 0) {
throw new IllegalArgumentException("Number must be positive");
}
}//如果num 小于 0,则抛出一个 IllegalArgumentException 异常。
throws 关键字用于在方法声明中指定该方法可能抛出的异常。当方法内部抛出指定类型的异常时,该异常会被传递给调用该方法的代码,并在该代码中处理异常。
public void readFile(String filePath) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
reader.close();
}/*当 readFile 方法内部发生 IOException 异常时,会将该异常传递给调用该方法的代码。在调用该方法的代码中,必须捕获或声明处理 IOException 异常。*/
//一个方法可以声明抛出多个异常,多个异常之间用逗号隔开
import java.io.*;
public class className
{public void withdraw(double amount) throws RemoteException,InsufficientFundsException
{
// Method implementation
}
//Remainder of class definition
}
实例:
throws 用在函数上,声明该函数的功能可能会出现问题。
可以抛给虚拟机处理,或者使用 try....catch... 进行处理。虚拟机的处理方式,就是将异常打印出来,并且将在异常处的代码终止。
throw 用在代码块中,后面跟着异常的对象,该对象可以是自定义异常,且 throw 使用在方法中,类似于调用构造函数的方法。
class FuShuException extends Exception //自定义异常
private int value;
FuShuException()
{
super();
}
FuShuException(String msg,int value)
{
super(msg);//调用父类有参构造,获得异常信息
this.value = value;
}
public int getValue()
{
return value;
}
}
class Demo
{
int div(int a,int b)throws FuShuException
{
if(b<0) {
// 手动通过throw关键字抛出一个自定义异常对象:FuShuException(String msg,int value)
throw new FuShuException("出现了除数是负数的情况------ / by fushu",b);
}
return a/b;
}
}
class ExceptionDemo
{
public static void main(String[] args)
{
Demo d = new Demo();
try
{
int x = d.div(4,-9);
System.out.println("x="+x);
}
catch (FuShuException e)
{
System.out.println(e.toString());
//System.out.println("除数出现负数了");
System.out.println("错误的负数是:"+e.getValue());
}
System.out.println("over");
}
}
finally 关键字用来创建在 try —catch代码块后面执行的代码块。
无论是否发生异常,finally 代码块中的代码总会被执行。
在 finally 代码块中,可以运行清理类型等收尾性质的语句。
finally 代码块出现在 catch 代码块最后,语法如下:
try{
// 程序代码
}catch(异常类型1 异常的变量名1){
// 程序代码
}catch(异常类型2 异常的变量名2){
// 程序代码
}finally{
// 程序代码
}注意:
- catch 不能独立于 try 存在。
- 在 try/catch 后面添加 finally 块并非强制性要求的。
- try 代码后不能既没 catch 块也没 finally 块。
- try, catch, finally 块之间不能添加任何代码。
- 在同一try...catch...finally块中 ,如果try中抛出异常,且有匹配的catch块,则先执行catch块,再执行finally块。如果没有catch块匹配,则先执行finally,然后去外面的调用者中寻找合适的catch块。
- finally中的return 会覆盖 try 或者catch中的返回值。
- finally中如果有return会抑制(消灭)前面try或者catch块中的异常。
- finally中如果写了异常会覆盖(消灭)前面try或者catch中的异常。
不要在fianlly中使用return,不要在finally中抛出异常,finally仅用来释放资源是最合适的,将所有的return写在函数的最后面,而不是try ... catch ... finally中。
public class ExcepTest{
public static void main(String args[]){
int a[] = new int[2];
try{
System.out.println("Access element three :" + a[3]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Exception thrown :" + e);
}
//无论你是否异常均执行语句
finally{
a[0] = 6;
System.out.println("First element value: " +a[0]);
System.out.println("The finally statement is executed");
}
}
}
/*
Exception thrown :java.lang.ArrayIndexOutOfBoundsException: 3
First element value: 6
The finally statement is executed
*/
在 Java 中你可以自定义异常。编写自己的异常类时需要记住下面的几点。
- 所有异常都必须是 Throwable 的子类。
- 如果希望写一个检查性异常类,则需要继承 Exception 类。
- 如果你想写一个运行时异常类,那么需要继承 RuntimeException 类。
例如:class MyException extends Exception{......}
import java.io.*;
//自定义异常类,继承Exception类
public class InsufficientFundsException extends Exception
{
//此处的amount用来储存当出现异常(取出钱多于余额时)所缺乏的钱
private double amount;
public InsufficientFundsException(double amount)
{
this.amount = amount;
}
public double getAmount()
{
return amount;
}
}
import java.io.*;
public class CheckingAccount
{
//balance为余额,number为卡号
private double balance;
private int number;
public CheckingAccount(int number)
{
this.number = number;
}
//方法:存钱
public void deposit(double amount)
{
balance += amount;
}
//方法:取钱
public void withdraw(double amount) throws
InsufficientFundsException
{
if(amount <= balance)
{
balance -= amount;
}
else
{
double needs = amount - balance;
throw new InsufficientFundsException(needs);
}
}
//方法:返回余额
public double getBalance()
{
return balance;
}
//方法:返回卡号
public int getNumber()
{
return number;
}
}
public class BankDemo
{
public static void main(String [] args)
{
CheckingAccount c = new CheckingAccount(101);
System.out.println("Depositing $500...");
c.deposit(500.00);
try
{
System.out.println("\nWithdrawing $100...");
c.withdraw(100.00);
System.out.println("\nWithdrawing $600...");
c.withdraw(600.00);
}catch(InsufficientFundsException e)
{
System.out.println("Sorry, but you are short $"
+ e.getAmount());
e.printStackTrace();
}
}
}
/*
Depositing $500...
Withdrawing $100...
Withdrawing $600...
Sorry, but you are short $200.0
InsufficientFundsException
at CheckingAccount.withdraw(CheckingAccount.java:25)
at BankDemo.main(BankDemo.java:13)
*/
在一些大型的,模块化的软件开发中,一个地方发生异常将导致一连串异常。假设B模块完成自己的逻辑需要调用A模块的方法,如果A模块发生异常,则B也将不能完成而发生异常,但是B在抛出异常时,会将A的异常信息掩盖掉,这将使得异常的根源信息丢失。异常的链化可以将多个模块的异常串联起来,使得异常信息不会丢失。
异常链化:以一个异常对象为参数构造新的异常对象。新的异对象将包含先前异常的信息。这项技术主要是异常类的一个带Throwable参数的函数来实现的。这个当做参数的异常,我们叫它根源异常(cause)。
实例:从命令行输入2个int,将他们相加,输出。输入的数不是int,则导致getInputNumbers异常,从而导致add函数异常,则可以在add函数中抛出一个链化的异常。
public static void main(String[] args){
System.out.println("请输入2个加数");
int result;
try{
result = add();
System.out.println("结果:"+result);
} catch (Exception e){
e.printStackTrace();
}
}
//获取输入的2个整数返回
private static List getInputNumbers(){
List nums = new ArrayList<>();
Scanner scan = new Scanner(System.in);
try {
int num1 = scan.nextInt();
int num2 = scan.nextInt();
nums.add(new Integer(num1));
nums.add(new Integer(num2));
}catch(InputMismatchException immExp){
throw immExp;
}finally {
scan.close();
}
return nums;
}
//执行加法计算
private static int add() throws Exception{
int result;
try {
List nums =getInputNumbers();
result = nums.get(0) + nums.get(1);
}catch(InputMismatchException immExp){
//public Error(String message, Throwable cause),关于cause是什么看第二点
throw new Exception("计算失败",immExp); //链化:以一个异常对象为参数构造新的异常对象。
}
return result;
}
/*
请输入2个加数
r 1
java.lang.Exception: 计算失败
at practise.ExceptionTest.add(ExceptionTest.java:53)
at practise.ExceptionTest.main(ExceptionTest.java:18)
Caused by: java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at practise.ExceptionTest.getInputNumbers(ExceptionTest.java:30)
at practise.ExceptionTest.add(ExceptionTest.java:48)
... 1 more
*/
参考资料:菜鸟教程 - 学的不仅是技术,更是梦想!
参考文献:最全最详细的Java异常处理机制_生成异常报告来协调两个接口系统-CSDN博客