关于JAVA异常资料翻译(二)

来自JAVA官方文档
原文:
The Catch or Specify Requirement
Valid Java programming language code must honor the Catch or Specify Requirement. This means that code that might throw certain exceptions must be enclosed by either of the following:

A try statement that catches the exception. The try must provide a handler for the exception, as described in Catching and Handling Exceptions.
A method that specifies that it can throw the exception. The method must provide a throws clause that lists the exception, as described in Specifying the Exceptions Thrown by a Method.
Code that fails to honor the Catch or Specify Requirement will not compile.

Not all exceptions are subject to the Catch or Specify Requirement. To understand why, we need to look at the three basic categories of exceptions, only one of which is subject to the Requirement.

The Three Kinds of Exceptions
The first kind of exception is the checked exception. These are exceptional conditions that a well-written application should anticipate and recover from. For example, suppose an application prompts a user for an input file name, then opens the file by passing the name to the constructor for java.io.FileReader. Normally, the user provides the name of an existing, readable file, so the construction of the FileReader object succeeds, and the execution of the application proceeds normally. But sometimes the user supplies the name of a nonexistent file, and the constructor throws java.io.FileNotFoundException. A well-written program will catch this exception and notify the user of the mistake, possibly prompting for a corrected file name.

Checked exceptions are subject to the Catch or Specify Requirement. All exceptions are checked exceptions, except for those indicated by Error, RuntimeException, and their subclasses.

The second kind of exception is the error. These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from. For example, suppose that an application successfully opens a file for input, but is unable to read the file because of a hardware or system malfunction. The unsuccessful read will throw java.io.IOError. An application might choose to catch this exception, in order to notify the user of the problem — but it also might make sense for the program to print a stack trace and exit.

Errors are not subject to the Catch or Specify Requirement. Errors are those exceptions indicated by Error and its subclasses.

The third kind of exception is the runtime exception. These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from. These usually indicate programming bugs, such as logic errors or improper use of an API. For example, consider the application described previously that passes a file name to the constructor for FileReader. If a logic error causes a null to be passed to the constructor, the constructor will throw NullPointerException. The application can catch this exception, but it probably makes more sense to eliminate the bug that caused the exception to occur.

Runtime exceptions are not subject to the Catch or Specify Requirement. Runtime exceptions are those indicated by RuntimeException and its subclasses.

Errors and runtime exceptions are collectively known as unchecked exceptions.

Bypassing Catch or Specify
Some programmers consider the Catch or Specify Requirement a serious flaw in the exception mechanism and bypass it by using unchecked exceptions in place of checked exceptions. In general, this is not recommended. The section Unchecked Exceptions — The Controversy talks about when it is appropriate to use unchecked exceptions.

译文:

捕获或者指定的要求

	有效的Java程序语言代码必须符合捕获或者指定的要求。这意味可能会抛出异常的代码一定会包含下面的一点或是几点:
	一个try关键字的捕获语句。try一定会给异常划分一个处理程序,如捕获和处理异常课程中所说。
	一个方法指定他可以抛出异常的方法。这个方法必须划分一个异常集合的trows语句,如指定因为方法抛出的异常课程中所所述。
	不符合捕获或者指定的要求的代码是无法编译的。
	不是所有异常都遵守捕获或者指定的要求。为了理解原因,我们需要看看三个基本的异常类别,其中只有一种符合要求。
	
	**三种异常**
	第一种异常是受检异常。这是一个写好的程序应该预料到并从中恢复的异常情况。举个例子,假设一个应用程序提示一个用户输入一个文件名,然后通过输入名字传递给java.io.FileReader的构造方法来打开该文件。一般情况下,用户会给现有的、可读的文件命名,因此filereader对象的结构创建成功了,应用程序的执行正常,但有时候用户命名了一个不存在的文件,构造方法就会抛出java.io.FileNotFoundException异常。一个写好的程序会捕获该异常然后通知给用户这个错误,也可能会提升输入正确的文件名。
	受检异常是遵守捕获和指定异常要求的。所有的异常都是受检异常,除了那些被Error, RuntimeException以及他们的子类标明的。
	第二种异常是错误。这是应用程序外部的意外情况,是应用程序通常不能预料和恢复的。举个例子,假设一个应用程序成功的打开了一个文件进行输入,但是因为硬件或者系统故障而不能读文件。不能成功读会抛出 java.io.IOError。一个应用程序可能会选择捕获这个异常,以便将问题通知给用户,但是程序打印堆栈跟踪然后退出也是有意义的。
	错误是不遵守捕获和指定异常要求的。错误是那些由错误或者子类指出的异常。
	第三种异常是运行时异常。这是应用程序内部的异常情况,应用程序通畅不能预料和恢复的。他们经常是程序的bug,像是逻辑错误或者不正确地使用API。举个例子,想象应用程序像前面我们描述的,给Filereader构造方法传递了一个文件名。如果一个逻辑错误的原因是传递了一个空值给构造方法,构造方法会抛出NullPointerException。应用程序会捕获这个异常,但是它可能会打印更多语句去排除这个异常发生的原因。
	错误和运行时异常都合称为非受检异常。
	**绕过捕获或者指定**
	有些程序员认为捕获或者指定要求在异常原理里是一个严重的缺陷,并且通过使用非受检异常去替代受检异常绕过了它。一般来说,这是不建议的。非受检异常-一个争议这节课程中会告诉我们什么时候适合使用非受检异常
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值