Possible Duplicate:
In Java, when should I create a checked exception, and when should it be a runtime exception?
我什么时候应该从 RuntimeException
而不是 Exception
派生异常?
RuntimeException
不必在方法的 throws
子句中声明,这可能好,因为它不必特别列出或不好,因为显式声明方法的异常是一种很好的做法。
想法?
来自 Unchecked Exceptions -- The Controversy :
If a client can reasonably be expected
to recover from an exception, make it
a checked exception. If a client
cannot do anything to recover from the
exception, make it an unchecked
exception.
请注意,未经检查的异常是从 RuntimeException
派生的异常。并且检查异常是从 Exception
派生的异常.
如果客户端无法从异常中恢复,为什么要抛出 RuntimeException
?文章解释:
Runtime exceptions represent problems
that are the result of a programming
problem, and as such, the API client
code cannot reasonably be expected to
recover from them or to handle them in
any way. Such problems include
arithmetic exceptions, such as
dividing by zero; pointer exceptions,
such as trying to access an object
through a null reference; and indexing
exceptions, such as attempting to
access an array element through an
index that is too large or too small.