When does the compiler require me to catch Exceptions?
class MyException extends Exception {
MyException() {}
MyException(String msg) { super(msg);}
}
public class NewException {
static void f() throws MyException {
System.out.println("throwing exception 开发者_开发问答from f()");
throw new ClassCastException();
}
static void g() throws MyException {
System.out.println("throwing exception from g()");
throw new MyException("parametrized ");
}
public static void main(String ...strings ) {
try {
f();
}
catch(MyException e) {
e.printStackTrace(System.out);
}
try {
g();
}
catch(MyException e) {
e.printStackTrace(System.out);
}
}
}
In the function f() I am specifying that "MyException " exception will be thrown and actually I am throwing some other exception which has no relation with MyException but still the compiler does not complain. Why?
ClassCastException
extends RuntimeException
, which means it is unchecked, and therefore the compiler does not require you to handle it.
From the Javadoc for RuntimeException
:
A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.
ide's answer is correct - only exceptions which are java.lang.Exception
or a subclass (direct or indirect) are "checked", and exceptions which are java.lang.RuntimeException
or a subclass are not checked. Only checked exceptions which are thrown from your method (including those declared to be thrown by methods you call, which you're not catching) need to be declared. This means that exceptions which aren't Exception
s (confusing, eh?) such as subclasses of Error
don't need to be declared either. (You can also subclass Throwable
directly, but you generally shouldn't.)
From the Java Language Specification, section 11.2:
A compiler for the Java programming language checks, at compile time, that a program contains handlers for checked exceptions, by analyzing which checked exceptions can result from execution of a method or constructor. For each checked exception which is a possible result, the throws clause for the method (§8.4.6) or constructor (§8.8.5) must mention the class of that exception or one of the superclasses of the class of that exception. This compile-time checking for the presence of exception handlers is designed to reduce the number of exceptions which are not properly handled.
The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses. All other exception classes are checked exception classes. The Java API defines a number of exception classes, both checked and unchecked. Additional exception classes, both checked and unchecked, may be declared by programmers. See §11.5 for a description of the exception class hierarchy and some of the exception classes defined by the Java API and Java virtual machine.
The checked exception classes named in the throws clause are part of the contract between the implementor and user of the method or constructor. The throws clause of an overriding method may not specify that this method will result in throwing any checked exception which the overridden method is not permitted, by its throws clause, to throw. When interfaces are involved, more than one method declaration may be overridden by a single overriding declaration. In this case, the overriding declaration must have a throws clause that is compatible with all the overridden declarations (§9.4).
精彩评论