看似不起波澜的日复一日 会突然在某一天让人看到坚持的意义 目录 1.认识异常 1.1 异常的概念 1.2 常见的异常 2.异常的体系结构 3.异常的分类 3.1 编译时异常 3.2 运行时异常 4.异常的处理 4.1 事前防
看似不起波澜的日复一日
会突然在某一天让人看到坚持的意义
目录
异常顾名思义(异:不同的 常:平常),不同于平常的就是异常
比如:有一天篮球哥上完厕所,当他按冲水键的时候,发现冲水键坏了,那么这就是异常
Java中异常:程序执行过程中发生的不正常行为称为异常
我们写代码时,常见的异常有算术异常、数组越界异常、空指针异常
①算术异常
System.out.println(10/0);
运行结果:
②数组越界异常
int[] arr = new int[]{1,2,3,4,5,6};System.out.println(arr[7]);
运行结果:
③空指针异常
int[] arr = null;System.out.println(arr.length);
运行结果:
从上述几个常见异常运行结果中可以看出,不同类型的异常,都有与其对应的类来进行描述
上述我们只是举了几个常见的异常,但是异常种类繁多,为了对不同异常或者错误进行很好的分类管理,Java内部维护了一个异常的体系结构:
上述讲的常见的异常都属于 Runtime Exception 运行时异常
Throwable:是异常体系中的顶层类,这个类派生出了两个子类一个是 Error(错误)和 Exception(异常)
异常可能在编译时发生错误,还可能在运行时发生错误,根据发生的时机不同,可以将异常分为两类:
编译时异常:在程序编译期间发生的异常,就叫做编译时异常,也称为受检查异常。会直接报错,除非处理掉这个异常,程序才会正常编译
class Person { private String name; private int age; @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } public static void main(String[] args) throws CloneNotSupportedException { Person person = new Person(); Person person1 = (Person)person.clone(); }}
如果不声明 throws CloneNotSupportedException,程序就不能正常编译。
throws CloneNotSupportedException 代表这个方法可能会出现异常,但是不进行处理,交给调用者进行处理。
在 Person 类中重写了父类 Object 里面的 clone 方法,并且声明了 throws CloneNotSupportedException 那么就是这个方法出现了异常,也不会进行处理。而是交给调用它的 main 方法进行处理,main 方法也声明了 throws CloneNotSupportedException,那么main方法也不会进行处理,也就交给调用 main 方法的处理,最后也就是交给了 JVM 去处理了
运行时异常:在程序运行期间发生的异常,就叫做运行时异常,也称为非受检查异常
RunTimeException以及其子类对应的异常,都称为运行时异常 。我们上述讲的算术异常、数组越界异常、空指针异常都是运行时异常。在写的时候并不会报错,在运行期间才会报错
注:编译时出现的语法性错误,不能称之为异常。比如你把一个关键字写错了此时编译过程中就会出错, 这是 "编译期" 出错。而运行时指的是程序已经编译通过得到 class 文件了, 再由 JVM 执行过程中出现的错误
异常的处理通常有两种:
事前防御型:就是用来防止出现异常,在操作之前就做充分的检查
class SignIn { private String userName = "abc"; private int passWord = 123; public void confirm(String userName,int password) { if (!(this.userName.equals(userName))) { System.out.println("用户名错误"); return; } if (!(this.password == password)) { System.out.println("密码错误"); return; } System.out.println("登录成功"); }}public class Test { public static void main(String[] args) { SignIn signIn = new SignIn(); signIn.confirm("aaa",123); }}
缺陷:正常流程和错误处理流程代码混在一起, 代码整体显的比较混乱。
事后认错型:也就是先操作, 遇到问题再处理
public class Test { public static void main(String[] args) { try { System.out.println(10/0); } catch (ArithmeticException e) { System.out.println("出现算术异常"); } System.out.println("程序继续执行"); }}
运行结果:
优势:正常流程和错误流程是分离开的, 程序员更关注正常流程,代码更清晰,容易理解代码
在Java中,异常处理主要的5个关键字:throw、try、catch、final、throws
在编写程序的时候,如果程序出现了错误,此时就需要将错误的信息告知给调用者。在Java中,通常借助 throw 关键字,抛出一个指定的异常对象,将错误信息告知给调用者
语法形式:
throw new XXXException("异常产生的原因");
异常抛出的代码:
public class Test { public static void fun(int[] arr,int index) { if (arr == null) { throw new NullPointerException("传递的数组为空"); } if (index < 0 || index >= arr.length) { throw new ArrayIndexOutOfBoundsException("下标越界"); } } public static void main(String[] args) { int[] arr = new int[]{1,2,3}; fun(arr,5); }}
运行结果:
注:
异常的捕捉是事后认错型的处理方式,主要有两种:异常声明 throws 以及 捕获处理 try-catch
1.异常声明throws
异常声明 处在 方法声明时参数列表之后 ,当方法中抛出编译时异常,用户不想处理该异常,此时就可以借助 throws 将异常抛给方法的调用者来处理。即当前方法不处理异常,提醒方法的调用者处理异常
语法格式:
修饰符 返回值类型 方法名(参数列表) throws 异常类型1,异常类型2...{}
异常声明throws代码:
class Person { private String name; private int age; @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } public static void main(String[] args) throws CloneNotSupportedException { Person person = new Person(); Person person1 = (Person)person.clone(); }}
运行结果:
注:
2. 捕获处理 try-catch
throws 对异常并没有真正处理,而是将异常报告给抛出异常方法的调用者,由调用者处理。如果真正要对异常进行处理,就需要 try-catch
public class Test { public static void fun(int[] arr,int index) { try { if (arr == null) { throw new NullPointerException("传递的数组为空"); } if (index < 0 || index >= arr.length) { throw new ArrayIndexOutOfBoundsException("下标越界"); } } catch (NullPointerException e) { e.printStackTrace(); } catch (ArrayIndexOutOfBoundsException e){ e.printStackTrace(); } } public static void main(String[] args) { int[] arr = new int[]{1,2,3}; fun(arr,5); System.out.println("代码继续执行"); }}
注:
在写程序时,有些特定的代码,不论程序是否发生异常,都需要执行。因为异常会引发程序的跳转,可能导致有些语句执行不到,finally 就是用来解决这个问题的
public class Test { public static void fun(int[] arr,int index) { try { if (arr == null) { throw new NullPointerException("传递的数组为空"); } if (index < 0 || index >= arr.length) { throw new ArrayIndexOutOfBoundsException("下标越界"); } } finally { System.out.println(123); } } public static void main(String[] args) { int[] arr = new int[]{1,2,3}; fun(arr,5); }}
运行结果:
注:finally中的代码一定会执行的,一般在finally中进行一些资源清理的扫尾工作
finally 执行的时机是在方法返回之前(try 或者 catch 中如果有 return 会在这个 return 之前执行 finally). 但是如果 finally 中也存在 return 语句, 那么就会执行 finally 中的 return, 从而不会执行到 try 中原有的 return
异常处理流程总结:
不同类型的异常,都有与其对应的类来进行描述。当我们在实际开发中所遇到的一些异常,此时就需要维护符合我们实际情况的异常结构,那我们就需要自定义异常
public class TwoException extends RuntimeException { public TwoException() { super(); } public TwoException(String str) { super(str); }}
public class Test { public static void fun(int index) { try { if (index == 2) { throw new TwoException("下标不为2"); } } catch (TwoException e){ e.printStackTrace(); } } public static void main(String[] args) { int[] arr = new int[]{1,2,3}; fun(2); System.out.println("程序继续执行"); }}
运行结果:
来源地址:https://blog.csdn.net/m0_66488562/article/details/127106424
--结束END--
本文标题: 【Java】异常
本文链接: https://www.lsjlt.com/news/403326.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0