java中两种异常类型

Errors are the bane of users and programmers alike. Developers obviously don't want their programs falling over at every turn and users are now so used to having errors in programs that they grudgingly accept to pay the price for software that will almost certainly have at least one error in it. Java is designed to give the programmer a sporting chance in designing an error-free application. There are exceptions that the programmer will know are a possibility when an application interacts with a resource or a user and these exceptions can be handled. Unfortunately, there are exceptions the programmer can't control or simply overlooks. In short, all exceptions are not created equal and therefore there are several types for a programmer to think about.

错误是用户和程序员的祸根。 开发人员显然不希望自己的程序崩溃,而用户现在已经习惯于在程序中出错,因此他们勉强接受为几乎肯定会出错的软件付出代价。 Java旨在为程序员提供一个设计无错应用程序的运动机会。 当应用程序与资源或用户进行交互时,程序员会知道存在一些例外,并且可以处理这些例外。 不幸的是,有一些程序员无法控制或完全忽略的例外。 简而言之,并不是所有的异常都一样,因此程序员可以考虑几种类型。

An exception is an event which causes the program to be unable to flow in its intended execution. There are three types of exception—the checked exception, the error and the runtime exception.

异常是导致程序无法按计划执行的事件。 异常分为三种:检查的异常,错误和运行时异常。

检查异常 ( The Checked Exception )

Checked exceptions are exceptions that a Java application should be able to cope with. For example, If an application reads data from a file it should be able to handle the FileNotFoundException . After all, there is no guarantee that the expected file is going to be where it is supposed to be. Anything could happen on the file system, which an application would have no clue about.

检查的异常是Java应用程序应能够应对的异常。 例如,如果应用程序从文件中读取数据,则它应该能够处理FileNotFoundException 。 毕竟,不能保证预期的文件将在预期的位置。 文件系统上可能发生任何事情,而应用程序则毫无头绪。

To take this example one step further. Let's say we are using the FileReader class to read a character file. If you have a look at the FileReader constructor definition in the Java api you will see it's method signature:

为了进一步说明这个例子。 假设我们正在使用FileReader类读取字符文件。 如果您查看Java api中的FileReader构造函数定义,则会看到它的方法签名:

public FileReader(String fileName)
throws FileNotFoundException

As you can see the constructor specifically states that the FileReader constructor can throw a FileNotFoundException. This makes sense as it's highly likely that the fileName String will be wrong from time to time. Look at the following code:

如您所见,构造函数特别声明FileReader构造函数可以抛出FileNotFoundException 。 这是有道理的,因为fileName字符串很可能不时出错。 看下面的代码:

public static void main(String[] args){
FileReader fileInput = null;
//Open the input file
fileInput = new FileReader("Untitled.txt");
}

Syntactically the statements are correct but this code will never compile. The compiler knows the FileReader constructor can throw a FileNotFoundException and it's up to the calling code to handle this exception. There are two choices - firstly we can pass the exception on from our method by specifying a throws clause too:

从语法上讲,这些语句是正确的,但是此代码永远不会编译。 编译器知道FileReader构造函数可以引发FileNotFoundException ,这取决于调用代码来处理此异常。 有两种选择-首先,我们也可以通过指定throws子句从方法中传递异常:

public static void main(String[] args) throws FileNotFoundException{
FileReader fileInput = null;
//Open the input file
fileInput = new FileReader("Untitled.txt");
}

Or we can actually handle with the exception:

或者我们可以实际处理以下异常:

public static void main(String[] args){
FileReader fileInput = null;
try
{
//Open the input file
fileInput = new FileReader("Untitled.txt");
}
catch(FileNotFoundException ex)
{
//tell the user to go and find the file
}
}

Well-written Java applications should be able to cope with checked exceptions.

编写良好的Java应用程序应该能够应对已检查的异常。

失误 ( Errors )

The second kind of exception is known as the error. When an exception occurs the JVM will create an exception object. These objects all derive from the Throwable class. The Throwable class has two main subclasses— Error and Exception. The Error class denotes an exception that an application is not likely to be able to deal with.

第二种异常称为错误。 发生异常时, JVM将创建一个异常对象。 这些对象都从Throwable类派生。 Throwable类有两个主要的子类: ErrorExceptionError类表示应用程序不太可能处理的异常。

These exceptions are considered rare. For example, the JVM might run out of resources due to the hardware not being able to cope with all the processes it is having to deal with. It's possible for the application to catch the error to notify the user but typically the application is going to have to close until the underlying problem is dealt with.

这些例外被认为是罕见的。 例如,由于硬件无法处理其必须处理的所有进程,JVM可能会耗尽资源。 应用程序可能会捕获错误以通知用户,但通常情况下,应用程序将不得不关闭,直到解决了根本问题为止。

运行时异常 ( Runtime Exceptions )

A runtime exception occurs simply because the programmer has made a mistake. You've written the code, it all looks good to the compiler and when you go to run the code, it falls over because it tried to access an element of an array that does not exist or a logic error caused a method to be called with a null value. Or any number of mistakes a programmer can make. But that's okay, we spot these exceptions by exhaustive testing, right?

发生运行时异常仅仅是因为程序员犯了一个错误。 您已经编写了代码,对于编译器来说一切都很好,并且在您运行代码时,它崩溃了,因为它试图访问不存在的数组元素,或者逻辑错误导致调用了方法具有空值。 或程序员可能犯的许多错误。 但是没关系,我们通过详尽的测试发现这些异常,对吗?

Errors and Runtime Exceptions fall into the category of unchecked exceptions.

错误和运行时异常属于未检查的异常类别。

翻译自: https://www.thoughtco.com/types-of-exceptions-2033910

java中两种异常类型

java中两种异常类型_Java中的三种异常类型相关推荐

  1. java7 javascript引擎_Java7中脚本引擎的一般用法,共三种方法获得JavaScript引擎:名称、文件扩展名、MIME类型 | 学步园...

    package com.sino.java7; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; i ...

  2. python中none算变量吗_在python中对变量判断是否为None的三种方法总结

    三种主要的写法有: 第一种:if X is None; 第二种:if not X: 当X为None,  False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()这 ...

  3. html没有注册类,电脑中ie浏览器提示没有注册类别的三种解决方法

    ie浏览器功能十分强大,能够给我们带来很棒的网页浏览体验.不过,一些朋友反馈自己在使用ie浏览器过程中,突然遇到"没有注册类别"的出错提示,这是怎么回事呢?其实这是文件的丢失等原因 ...

  4. jupyter notebook python3路径_详解修改Anaconda中的Jupyter Notebook默认工作路径的三种方式...

    方式1. 打开Windows的cmd,在cmd中输入jupyter notebook --generate-config如下图: 可以看到路径为D:\Users--找到此路径修改jupyter_not ...

  5. JavaScript中遍历数组的for for-in和forEach三种方式

    JavaScript中遍历数组的for for-in和forEach三种方式 for循环 let arr = [1,2,3,4,5,6];for(let i = 0; i < arr.lengt ...

  6. java解析遍历List集合(其实现子类)的三种方式

    java解析遍历List集合(其实现子类)的三种方式 1 使用迭代器对象 1.1 底层 1.1.1 List接口继承了Collection接口 1.1.2 而Collection接口又继承了Itera ...

  7. 使用构造方法 重载 Scanner键盘录入的方式,做一个两个int类型的相加 和三个double类型的计算器

    import java.util.Scanner; public class Calculator { // 使用构造方法 重载 Scanner键盘录入的方式,做一个两个int类型的相加 和三个dou ...

  8. 数据库时间内接受的是lang类型的时间 分为三种字段 第一种只存日期 第二种存日期+时间 第三种时间戳...

    数据库时间内接受的是lang类型的时间 分为三种字段 第一种只存日期 第二种存日期+时间 第三种时间戳 转载于:https://www.cnblogs.com/classmethond/p/10250 ...

  9. java ref 应用类型_Java中的四种引用类型比较

    1.引用的概念 引用这个概念是与JAVA虚拟机的垃圾回收有关的,不同的引用类型对应不同的垃圾回收策略或时机. 垃圾收集可能是大家感到难于理解的较难的概念之一,因为它并不能总是毫无遗漏地解决Java运行 ...

最新文章

  1. Mybaits整合Spring自动扫描 接口,Mybaits配置文件.xml文件和Dao实体类
  2. mysql useradd_useradd失败
  3. Input中onbeforepaste的作用
  4. Linux ARP代理 与 NAT
  5. python爬虫经典段子_Python爬虫实战(1):爬取糗事百科段子
  6. oracle dblink 验证,Oracle DBLINK 简单使用
  7. k3 xp服务器系统,如何在英文XP环境下安装k3系统.doc
  8. 记一次zookeeper连接数暴增事件
  9. 工具记录,使用jarsigner 对APK进行签名
  10. JPA Example查询
  11. python ** 运算符_Python学习第二天--运算符小结
  12. 监控sqlserver 数据变化并记录_携程机票数据仓库11年技术栈的演进
  13. 【器件知识】【设计】ESD专题-闩锁效应-大尺寸输出缓冲器
  14. VS自带数据库SqlExpress
  15. 空间坐标系对应EPSG编号
  16. shenyu自定义插件
  17. 游戏模型制作的注意事项 项目模型规范总结
  18. 巨头发力,社区电子商务发展加速
  19. Ubuntu 20.04安装绿联PL2303串口驱动
  20. 新三板开通精选层后,仅存的四家软件公司都是做什么的?

热门文章

  1. 美元汇率【贪心算法练习题】
  2. kodi remote android,使用Android和iOS在Win10系统中设置Kodi Remote方法
  3. 安卓Bmob后端云的使用(增删改查、上传图片、推送服务等)
  4. java-jacob操作word,往word中动态插入各种文件
  5. python-docx结合lxml读取word文档段落格式、字体格式等信息
  6. 天翼数字生活C++客户端实习
  7. QNX系统挂载CF卡
  8. 云渲染技术在虚拟仿真医疗培训中的应用
  9. mysql5.6 rpm安装配置
  10. 三至六世紀浙東地區的經濟發展