当前位置:网站首页>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();
}
边栏推荐
- Super handy drawing tool is recommended
- c语言结构体中的冒泡排序
- How to prepare for the test interface test data
- User password verification
- IO process thread -> thread -> day5
- BIOTIN ALKYNE CAS:773888-45-2价格,供应商
- Common fluorescent dyes to modify a variety of groups and its excitation and emission wavelength data in the data
- 【Harmony OS】【FAQ】鸿蒙问题合集1
- 建立树形结构
- BIOTIN ALKYNE CAS: 773888-45-2 Price, Supplier
猜你喜欢

DFS对剪枝的补充

Interface Test Framework Practice (4) | Get Schema Assertion

IO process thread -> thread -> day5

Online password generator tool recommendation

MCM箱模型建模方法及大气O3来源解析

shell script loop statement

13.< tag-动态规划和回文字串>lt.647. 回文子串 + lt.516.最长回文子序列

接口测试实战| GET/POST 请求区别详解

typescript45-接口之间的兼容性

Where is the value of testers
随机推荐
【 Harmony OS 】 【 ano UI 】 lightweight data storage
High availability, two locations and three centers
Shell conditional statement judgment
MySQL 出现 The table is full 的解决方法
【生物素叠氮化物|cas:908007-17-0】价格_厂家
业务表解析-余额系统
在竞争白热化的电商行业,链动2+1为什么还有企业在用
数字孪生园区场景中的坐标知识
typescript40-class类的保护修饰符
3. 无重复字符的最长子串
Two ways to simulate multi-user login in Jmeter
Flink state
WinForm的控件二次开发
UV decomposition of biotin - PEG2 - azide | CAS: 1192802-98-4 biotin connectors
DFS's complement to pruning
Unity2D horizontal board game tutorial 6 - enemy AI and attack animation
Apache DolphinScheduler版本2.0.5分布式集群的安装
typescript46-函数之间的类型兼容性
接口测试框架实战(二)| 接口请求断言
Redis连接不上的报错解决方案汇总