开发者

Java: how can an invoking function not handle an exception from sub-function?

I learnt an invoking function must either declare or handle all the exc开发者_JS百科eptions the invoked function declares to throw. But following code can compile.

public a() throws SipException, NullPointerException {
    try { }
    catch (SipException e) { throw e; }
    catch (Exception e) { throw new SipException("...", e); }
}
public b() throws SipException { a(); }

since a declares to throw NullPointerException, though it does not really do, how can b ignore this please?


Java differentiates betwixt exceptions that have to be declared or caught, or those that don't. The latter are called unchecked exceptions.

NullPointerException is an unchecked exception, which does not need to be declared or caught (so you should remove it from your throws line.

Think about it, null pointers can happen almost anywhere in your code; if you had to declare or catch them, every single method would have to do this.

Or as this says

Those unchecked exception classes which are the error classes (Error and its subclasses) are exempted from compile-time checking because they can occur at many points in the program and recovery from them is difficult or impossible. A program declaring such exceptions would be cluttered, pointlessly.


an invoking function must either declare or handle all the exceptions the invoked function declares to throw.

The exception here are RuntimeExceptions, which you do not need to declare. NullPointerException is a RuntimeException. Non-RuntimeExceptions are called "checked exceptions" (because the compiler checks that you think about them).


There are two main kinds of exceptions: RuntimeException and regular plain-old Exception. You only need to explicitly handle Exceptions. RuntimeExceptions may be ignored and will propagate up the stack exactly as though you wrote "throws NullPointerException" in every signature.

There are various philosophies about when to use each type. I subscribe to the idea that RuntimeExceptions should represent coding errors, while checked exceptions represent problems that are "expected" to occur in the somewhat normal flow of the running program.


In Java, there are two types of exceptions which do not need to be explicitly caught:

  1. Any exception which extends java.lang.Error
  2. Any exception which extends java.lang.RuntimeException

These two exceptions exist to denote exceptions which are indicative of fatal errors. Errors which cannot be recovered from should not be caught in the first place, rather the programmer should let the program fail. So the language allows you to omit the annoying try {} catch {} boilerplate in these situations.


Look at Checked vs Unchecked Exceptions. http://www.javapractices.com/topic/TopicAction.do?Id=129 NPE is an unchecked exception, imagine the verbosity of programs if everything had to either catch or declare all possible exceptions!

RuntimeExceptions (Unchecked Exceptions) are a key concept to understand. Check out the javadoc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜