Code: Select all
1. class A{
2. String value = "test";
3. A(String val){
4. this.value = val;
5. }
6. }
7. public class TestClass {
8. public static void main(String[] args) throws Exception {
9. new A("new test").print();
10. }
11.}
Why? Because of two rules I learned (based on Sierra & Bates' book):
A) If a method invokes a checked exception (from the Exception-class), this exception must either be handled (using try-catch) or declared (using throws in its method). If neither happens, a compiler error will follow.
B) An uncaught exception will propagate through the call stack until the exception is either caught, or the exception reaches the main()-method. If main() also "ducks" the exception by declaring it (using throws) the JVM will shut down and a stack trace will be printed.
Now, in this code a checked exception will be thrown at line 9. This exception will then be declared using throws in the main()-method. Therefore, the main()-method "ducks" the exception, and ultimately the JVM will shut down and a stack trace will be printed (but no compiler error).
Am I incorrect? Is a JVM shutdown and a printout of the stack trace the same as a compiler error?