1. Java Exception 介绍
1.1 什么是java异常
1.java异常是程序运行时遇到不被期望的事件,影响了程序的正常的运行。
2.异常的分类:
- 语法编译错误;
- 程序运行的时候错误;
- 程序逻辑错误:没有按照程序正常逻辑运行;
3.Throwable类是java.lang包的一个类(java.lang.Throwable),是所有异常的超类,Throwable包含了2大子类error、Exception;
- error 程序无法自己处理的错误,例如:OutofmemoryError(内存溢出错误)
- Exception 程序自己可以处理的错误 例如:ArrayIndexOutOfBoundException异常;
4.异常可以分为可见异常和不可见异常
- 可见异常:RuntimeException类及其子类之外的异常例如:
- ClassNotFindException 类没有找到;
- illegalaccessException 类不被允许进入;
- NoSuchFiedException 没有这个变量;
- NoSuchMethodException 没有这个方法;
- 不可见异常:RuntimeException类及其子类和错误的异常例如:
- ArrayIndexOutOfBoundException 数据越界;
- ClasscastException 类强制转换失败
- NullpointerException 声明一个对象,没有实例化,调用的报错
- NumberFormalException 数据类型转换失败
1.2 Exception的常用方法
//获取异常信息
public String getmessage(){};
//获取异常的原因
public Throwable getcause(){}
//打印堆栈信息,错误流,可以看到错误的原因和位置
public String printStackTrace(){}
//打印堆栈层次的数组,栈顶为0,栈底为最后一个元素
public StackTraceElement getStackTrace(){}
2 Java异常的捕获和抛出
2.1 try…catch 捕获异常
- 用于捕获try代码块的异常
- 后面可以跟上多个catch,如果try里面异常不在catch捕获的范围则会打印堆栈信息
catch范围的捕获
public class Test1 {
public static void main(String[] args) {
//定义一个数组
String [] strings={"大一","大二","大三","大四"};
try {
//打印数组(给数组长度加1使数组打印越界)
for (int i = 0; i < strings.length+1; i++) {
System.out.println(strings[i]);
}
//捕获数据越界异常
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("数据越界了");
}
}
}
结果
catch范围外的捕获(直接打印栈堆信息)
public class Test1 {
public static void main(String[] args) {
//定义一个数组
String [] strings={"大一","大二","大三","大四"};
try {
//打印数组(给数组长度加1使数组打印越界)
for (int i = 0; i < strings.length+1; i++) {
System.out.println(strings[i]);
}
//捕获空指针异常,而不捕获数据越界的异常
}catch (NullPointerException e){
System.out.println("空指针异常");
}
}
}
结果
catch 多重捕获
- 代码中发生异常,异常被抛给第⼀个 catch 块, 如果不匹配则继续往下⼀个catch进行传递
public class Test1 {
public static void main(String[] args) {
//定义一个数组
String [] strings={"大一","大二","大三","大四"};
try {
//打印数组(给数组长度加1使数组打印越界)
for (int i = 0; i < strings.length+1; i++) {
System.out.println(strings[i]);
}
//捕获数据越界异常
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("数据越界了");
//空指针异常
}catch(NullPointerException e){
System.out.println("空指针异常");
}
}
}
结果
2.2 finally关键字
- finally后面的代码总会被执行,异常也不会被中止
- finally和try…catch的组合方式有 try …finally,try…catch…finally,try…catch…catch…finally
public class Test1 {
public static void main(String[] args) {
//定义一个数组
String [] strings={"大一","大二","大三","大四"};
try {
//打印数组(给数组长度加1使数组打印越界)
for (int i = 0; i < strings.length+1; i++) {
System.out.println(strings[i]);
}
//捕获数据越界异常
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("数据越界了");
//空指针异常
}finally {
System.out.println("finally被打印了");
}
}
}
finally不要定义返回值,如果定义了返回值会先于try得返回
public class Test1 {
public static void main(String[] args) {
int i = printArr();
System.out.println(i);
}
//定义一个数组
public static int printArr() {
//定义一个数组
String[] strings = {"大一", "大二", "大三", "大四"};
try {
//打印数组(给数组长度加1使数组打印越界)
for (int i = 0; i < strings.length + 1; i++) {
System.out.println(strings[i]);
}
//try返回得是1
return 1;
//捕获数据越界异常
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数据越界了");
//空指针异常
} finally {
System.out.println("finally被打印了");
//finally返回得是-1
return -1;
}
}
}
2.3关键字throws,thow
2.3.1 throws
- 向外抛出异常,throws跟在参数列表后面,可以抛出多个异常用逗号隔开
public class Test1 {
public static void main(String[] args) {
printArr();
}
//定义一个数组
//向外抛出异常
public static void printArr() throws ArrayIndexOutOfBoundsException,NullPointerException{
//定义一个数组
String[] strings = {"大一", "大二", "大三", "大四"};
//打印数组(给数组长度加1使数组打印越界)
for (int i = 0; i < strings.length + 1; i++) {
System.out.println(strings[i]);
}
//try返回得是1
}
}
2.3.2 throw关键字
- try…catch捕获异常后自己处理或者继续向上抛出异常使用throw
public class Test1 {
public static void main(String[] args) {
try {
printArr();
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("数据越界了");
}
}
//定义一个数组
//向外抛出异常
public static void printArr(){
//定义一个数组
String[] strings = {"大一", "大二", "大三", "大四"};
try {
//打印数组(给数组长度加1使数组打印越界)
for (int i = 0; i < strings.length + 1; i++) {
System.out.println(strings[i]);
}
}catch(ArrayIndexOutOfBoundsException e){
//向外抛出一个异常处理
throw new ArrayIndexOutOfBoundsException();
}
}
}
3 自定义一个异常
- Java的内置异常不能满足的时候。
- 自定义异常需要继承Exception类。
定义异常
//自定义一个参数异常
public class ParameterException extends Exception {
//设置错误码
private int code;
//设置错误参数
private String msg;
public ParameterException() {
super();
}
public ParameterException(int code,String msg) {
super(msg);
this.code=code;
this.msg=msg;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
调用异常
public class Test {
public static void main(String[] args) {
//捕获异常并抛出异常
try {
print("a");
}catch (ParameterException e){
System.out.println("123");
System.out.println(
"错误码是:"+e.getCode()+"\n"+"错误信息是:"+e.getMsg()
);
}
}
//必须传入参数abc //向方法外抛出错误
public static void print(String a) throws ParameterException {
if(a.equals("abc")){
System.out.println("输入正确");
}else{
throw new ParameterException(10001,"参数输入错误");
}
};
}