当前位置:网站首页>Exception (abnormal) and Error (error) difference analysis
Exception (abnormal) and Error (error) difference analysis
2022-08-03 05:12:00 【hard work boy】
Exception和ErrorAll come from a common ancestor —— java.lang 包中的 Throwable 类.
Error(错误):是程序无法处理的错误,表示运行应用程序中较严重问题.
大多数错误It doesn't matter what the code writer does,而表示代码运行时 JVM(Java 虚拟机)出现的问题.例如,Java 虚拟机运行错误(Virtual MachineError),当 JVM There are no longer memory resources required to continue the operation时,将出现OutOfMemoryError.这些异常发生时,Java 虚拟机(JVM)一般会选择线程终止.这些错误表示故障发生于虚拟机自身、或者发生在虚拟机试图执行应用时,如 Java 虚拟机运行错误(VirtualMachineError)、类定义错误(NoClassDefFoundError)等.这些错误是不可查的,因为它们在应用程序的控制和处理能力之外,而且绝大多数是程序运行时不允许出现的状况.对于设计合理的应用程序来说,即使确实发生了错误,本质上也不应该试图去处理它所引起的异常状况.在 Java中,错误通过 Error 的子类描述.
Exception(异常)
Exception(异常):程序本身可以处理的异常.Exception 类有一个重要的子类RuntimeException.RuntimeException 异常由 Java 虚拟机抛出.NullPointerException(要访问的变量没有引用任何对象时,抛出该异常)、ArithmeticException(算术运算异常,一个整数除以 0时,抛出该异常)和 ArrayIndexOutOfBoundsException (下标越界异常).
异常和错误的区别:异常能被程序本身处理,错误是无法处理
Common ways to get error information
public string getMessage() :返回异常发生时的简要描述
public string toString() :返回异常发生时的详细信息
public string getLocalizedMessage() :返回异常对象的本地化信息.使用 Throwable 的子类覆盖这个方法,可以生成本地化信息.如果子类没有覆盖该方法,则该方法返回的信息与getMessage() 返回的结果相同
public void printStackTrace() :在控制台上打印 Throwable 对象封装的异常信息.
try-catch-finally
try : 捕获异常.其后可接零个或多个 catch ,如果没有 catch ,则必须跟一个 finally.
catch : 处理 try 捕获到的异常.
finally : 无论是否捕获或处理异常,finally The statements in it will be executed.当在 try 或 catch 中遇到 return 语句时,finally The statement will be executed before the method returns.
以下 4 种特殊情况下,finally 不会被执行:
- 在 finally 语句块第一行发生了异常. in other lines,finally will still be implemented
- 在前面的代码中用了 System.exit(int)已退出程序. exit 是带参函数 ;若该语句在异常语句之后,finally 会执行
- 程序所在的线程死亡.
- 关闭 CPU.
try 语句和 finally 语句中都有 return 语句时,在方法返回之前,finally 语句的内容将被执
行,并且 finally 语句的返回值将会覆盖原始的返回值.
try-with-resources代替try-catch-finally
中类似于 InputStream 、 OutputStream 、 Scanner 、 PrintWriter 等的资源都需要我们调用close() 方法来手动关闭,一般情况下我们都是通过 try-catch-finally 语句来实现这个需求.
try-with-resources 语句让我们更容易编写必须要关闭的资源的代码.
try-with-resources The statement is to put the originaltry-catchThe execute statement in the try中,类似于作为try的参数,而不是作为try块的内容,如下:
try (Scanner scanner = new Scanner(new File("test.txt"))) {
while (scanner.hasNext()) {
System.out.println(scanner.nextLine());
}
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
边栏推荐
- Interface test Mock combat (2) | Combined with jq to complete batch manual Mock
- UV 裂解的生物素-PEG2-叠氮|CAS:1192802-98-4生物素接头
- WebSocket的实际应用
- [Fine talk] Using native js to implement todolist
- typescript41-class类的私有修饰符
- Coordinate knowledge in digital twin campus scenarios
- Modified BiotinDIAZO-Biotin-PEG3-DBCO|diazo-biotin-tripolyethylene glycol-diphenylcyclooctyne
- 【Harmony OS】【ArkUI】ets开发 基础页面布局与数据连接
- tag单调栈-单调栈预备知识-lt.739. 每日温度
- Shell conditional statement judgment
猜你喜欢
随机推荐
typescript47-函数之间的类型兼容性
C# async and multithreading
在树莓派上搭建属于自己的网页(1)
Kotlin-Flow常用封装类:StateFlow的使用
js garbage collection mechanism
Tributyl-mercaptophosphane "tBuBrettPhos Pd(allyl)" OTf), 1798782-17-8
MySql 创建索引
2022暑假牛客多校联赛第一场
设计模式——组合模式、享元模式(Integer缓存)(结构型模式)
tag单调栈-单调栈预备知识-lt.739. 每日温度
typescript39-class类的可见修饰符
js的垃圾回收机制
修饰生物素DIAZO-生物素-PEG3-DBCO|重氮-生物素-三聚乙二醇-二苯基环辛炔
GIS数据漫谈(六)— 投影坐标系统
2022/08/02 学习笔记 (day22) 多线程
Fluorescent marker peptides FITC/AMC/FAM/Rhodamine TAMRA/Cy3 / Cy5 / Cy7 - Peptide
接口和协议
OSI的分层特点、传输过程与三次握手、四次挥手、tcp与udp包头的描述
WebSocket的实际应用
Bubble sort in c language structure








