java中两种异常类型_Java中的三种异常类型

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中两种异常类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值