posted 22 years ago
I don't understand when I need to include the "throws" clause in the method declaration. According to the books I have been reading, I don't need it when it is a Runtime Exception, but I need it for checked exceptions. My problem is that I cannot seem to distinguish between the 2 types and also cannot determine what kind of an exception is being thrown.
For eg : in this code here, which lines will cause an error.
1. class A {
2. void m1() {throw new ArithmeticException();}
3. void m2() {throw new ClassCastException();}
4. void m3() {throw new IllegalArgumentException();}
5. void m4() {throw new IndexOutOfBoundsException();}
6. void m5() {throw new NullPointerException();}
7. void m6() {throw new SecurityException();}
8. }
What is the result of attempting to compile the program?
a. Compiler error at line 2.
b. Compiler error at line 3.
c. Compiler error at line 4.
d. Compiler error at line 5.
e. Compiler error at line 6.
f. Compiler error at line 7.
g. None of the Above
Can anyone explain this?
Thanks
Sharda