通常的,errors能够分为如下几类:java
一种经常使用的错误处理方法是返回error code,由calling method根据error code作不一样处理。安全
可是有些情形下error code并不方便使用,好比如何取分错误码和与错误码相同的有效值。app
The mission of exception handling is to transfer control from where the error occured to an error handler that can deal with the situation.ide
当出现错误时,程序应当恢复到某个安全状态并从新来过,或者保存当前工做安全退出,可是这并不容易,关键就是程序控制权如何从触发错误的地方跳转处处理错误的地方。this
Java allows every method an alternative exit path if it is unable to complete its task in the normal way:编码
Throwable是整个Java exception hierarchy的基类,其下分为:code
the error hierarchy describes internal errors and resource exhaustion situations inside the Java runtime system.orm
You should not throw an object of this type. There is little you can do if sucn an internal error occurs, beyond notifying the user and trying to terminate the program gracefully.ip
Exception能够分为两类:input
RuntimeException意味着编码错误,好比
a bad cast, an out-of-bounds array access, a null pointer access等。
其余Exception通常是出现了某种意外,some bad things happened, 好比,打开一个不存在的文件。
为何打开不存在的文件不是RuntimeException?由于这不是你的代码能控制的,你先校验文件是否存在也没用,可能你校验时是存在的,但你打开时就不存在了。
Error和RuntimeException这两支,咱们称为unchecked exception.
除Error和RuntimeException这两支外的其余Exception,咱们称为checked exception.
The compiler checks that you provide exception handlers for all checked exceptions:
对于你的方法可能抛出的checked exceptions,你必须在method declaration中经过throws声明出来。
若是可能抛出多个checked exceptions,那么须要都列出来,使用逗号分隔。
public Image loadImage(String name) throws FileNotFoundException, EOFException {...}
注意,unchecked exception不该当出如今你的throws声明中:
如何抛出exception呢?很简单:
没有合适的standard exception class可用?没有关系,你能够自定义一个
class FileFormatException extends IOException { public FileFormatException() {} public FileFormatException(String gripe) { super(gripe); } }
通常的,咱们为自定义的exception class提供两个constructors:a default constructor and a constructor with a detailed message.
try { xxx } catch (ExceptionType1 | ExceptionType 2 e) { xxx } catch (ExceptionType3 e) { xxx }
对于checked exceptions,若是你知道如何处理它们,那么你能够catch它们,这样就不用抛出它们了。
As a general rule, you should catch those exceptions that you know how to handle and propagate those that you do not know how to handle.
try { xxx } catch (SQLException e) { throw new ServletException("xxx error").initCause(e); }
这是rethrow exception的经常使用方式。经过这种方式,咱们能够包装出一个更抽象的exception,或者把一个checked exception转换成一个RuntimeException.
try { try { xxx } finally { xxx } } catch (Exception e) { xxx }
the inner try block has a single responsibility: to make sure that the resources are released
the outer try block has a single responsibility: to ensure that errors are reported.
注意,the body of the finally clause is intended for cleaning up resources. Don't put statements that change the control flow (return, throw, break, continue) inside a finally clause.
try (Resource res = xxx) { xxx }
从Java 7开始,try-finally结构能够简化为try-with-Resources.
要求Resource必须是AutoCloseable的实现类,when the try block exits, then res.close() is called automatically.
A difficulty arises when the try block throws an exception and the close method also throws an exception. The original exception is rethrown, and any exceptions thrown by close methods are considered "suppressed". They are automatically caught and added to the original exception with the addSuppressed method.