Question

From Effective Java "DOCUMENT ALL EXCEPTIONS THROWN BY EACH METHOD"

It should be noted that documenting all of the unchecked exceptions that each method can throw is an ideal, not always achievable in the real world. When a class undergoes revision, it is not a violation of source or binary compatibility if an exported method is modified to throw additional unchecked exceptions. Suppose a class invokes a method from another, independently written class. The authors of the former class may carefully document all of the unchecked exceptions that each method throws, but if the latter class is revised to throw additional unchecked exceptions, it is quite likely that the former class (which has not undergone revision) will propagate the new unchecked exceptions even though it does not declare them.

I fail to understand how can former class propagate the new unchecked excpetions ? The java language doesn't mandate the caller to catch and propagate unchecked exceptions .

Was it helpful?

Solution

It will propagate it precisely because exceptions are either caught or propagated. If not caught, an exception propagates:

public void caller() {
    callee();
}

public void callee() {
    throw new RuntimeException();
}

In the above example, the exception thrown from callee() will be propagated by the caller() method, since caller() doesn't catch it.

OTHER TIPS

Well, if you don't catch the unchecked exceptions they propagate automatically.

@Geek ... yes java language does not mandate to handle unchecked exceptions and if you do not handle any unchecked exceptions. then Java Virtual Machine(JVM) will handle all the exception thrown by the program execution.

If called method does not handle Exception, then Exception will propagate to Calling method. If Calling method does not handle Exception, then Exception will propagate to Java Virtual Machine and default exception handler will come into effect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top