# Java异常
## 概述
1. 异常就是指程序执行报错或者出现了不正常现象
2. 如果程序出现了问题,没有做任何处理,Jvm最终会做默认处理。 并将异常的名称,异常的原因及异常出现的位置等信息输出在控制台。
## 异常的处理(try.catch)
### 1. try.catch 格式
“`
1. try … catch…
try{
可能出现异常的代码
}catch(异常类名 变量名){
异常的处理代码
}
“`
### 2. 执行流程:
1. 程序从try里面的代码开始执行,当出现异常,会自动生成一个异常类对象,该异常对象将被提交给Java运行时系统。
2. 当Java运行时系统接收到异常对象时,会到catch中去找匹配的异常类,找到后进行异常的处理。
3. 执行完毕后,程序还可以继续往下执行。
“`java
public static void main(String[] args) {
System.out.println("开始");
try {
method();
} catch (IndexOutOfBoundsException e) {
//程序在运行时,如果发生报错,查找IndexOutOFBoundException
//捕获成功
System.out.println("下标越界");
}
method();
System.out.println("结束 ");
}
public static void method() {
int[] arr = {1, 2, 3,};
System.out.println(arr[3]);
}
“`
## catch 顺序
程序报错后,会在catch中查找匹配异常对象
1. 如果捕获到了,程序会继续执行
2. 如果没有捕获到,程序会中断
“`java
public static void main(String[] args) {
System.out.println("开始");
try {
method();
} catch (ArrayIndexOutOfBoundsException e) {
//程序在运行时,如果发生报错,查找IndexOutOFBoundException
//捕获成功
System.out.println("下标越界");
} catch (NumberFormatException e1) {
System.out.println("类型转换异常");
}
System.out.println("结束 ");
}
public static void method() {
String str = "this is a TestString";
int[] arr = {1, 2, 3,};
int num = Integer.parseInt(str);
System.out.println(arr[3]);
System.out.println(num);
}
“`
**通常情况下,如果用代码分别进行捕获,会导致开发效率的降低和代码量过于繁杂**
虽然可以做简化,但也无法知道具体异常类型
“`java
public static void main(String[] args) {
System.out.println("开始");
try {
method();
} catch (Exception e) {
System.out.println("程序错误");
}
System.out.println("结束 ");
}
public static void method() {
String str = "this is a TestString";
int[] arr = {1, 2, 3,};
int num = Integer.parseInt(str);
System.out.println(num);
System.out.println(arr[3]);
}
“`
**这时就引出了 *Throwable* 类,类异常及其子类是Throwable的一种形式**
“`printStackTrace()“`是Throwable的类方法,作用是把异常的错误信息输出在控制台
例如:
 {
System.out.println("开始");
try {
method();
} catch (ArrayIndexOutOfBoundsException e1) {
System.out.println("单独输出,数组异常");
} catch (Exception e2) {
System.out.println("程序错误");
//在结束后,输出异常发生的位置
e.printStackTrace();
}
System.out.println("结束 ");
}
public static void method() {
String str = "this is a TestString";
int[] arr = {1, 2, 3,};
int num = Integer.parseInt(str);
System.out.println(num);
System.out.println(arr[3]);
}
“`
## 小结
在需要处理众多异常中的某特定异常时
可以先使用try catch来捕获特定异常,之后用异常的父类Exception来捕获其他异常
主要,在捕获异常时,需要捕获的特定异常需要放在前面。
因为在捕获异常时,如果先捕获了父类异常,则无法再捕获子类异常。
## Throwable的成员方法
“`
public String getMessage 返回此throwable的详细信息字符串
public String toString 返回此可抛出的简短描述
public void printStackTrace() 把异常的错误信息输出在控制台
“`
## 自定义异常
在java中可以自定义异常,需要继承异常类Exception(RuntimeException),提供无参构造和有参构造,把有参构造的参数传递给父类
例如:
“`java
import java.nio.file.Path;
class PathNotExistException extends Exception {
public PathNotExistException() {
}
//有参构造方法
public PathNotException(String message) {
super(message);
}
}
“`
## 关键字finally
finally——表示无论是否出现异,{ }中的代码一定会执行
finally可以和try…catch使用,也可以单独和try使用
“`java
public static void main(String[] args) {
// System.out.println(method());
}
public static int method1() {
//输出结果为2
try {
return 1;
} finally {
return 2;
}
}
public static int method2() {
//输出结果为3
try {
return 1;
} finally {
try {
return 2;
} finally {
return 3;
}
}
}
//此方法先进入try代码块中,先执行return i, 再执行i++;
//所以先返回的是i=10,i++,因此i=11
//继续执行finally的代码。
//i++ i=12。
//所以结果为,method3的返回值为10,而i=12
public static int method3() {
int i = 10;
try {
return i++;
} finally {
i++;
System.out.println("i = " + i);
}
}
“`