permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception Problem" Watch "Exception Problem" New topic
Author

Exception Problem

Jyotsna Umesh
Ranch Hand

Joined: May 09, 2001
Posts: 94
Hi JavaRanchers,
Please explain why the answer to this problem is compilation error?
public class ThrowsDemo {
static void throwMethod() {
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwMethod();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
}
}
[a] Compilation error
[b] Runtime error
[c] Compile successfully, nothing is printed.
[d] inside demoMethod. followed by caught: java.lang.IllegalAccessException: demo
Thanx
Jyotsna
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Jyotsna,
If a method can or does throw a checked Exception, then it must either declare that it does via a "throws" clause in the method signature

or else it must place the statement which may throw a checked exception within a try-catch block

The code you posted does neither, so the compiler complains.
Jyotsna Umesh
Ranch Hand

Joined: May 09, 2001
Posts: 94
Thanks Scott for the explanation, its silly of me, i didn't check this.
Jyotsna
Originally posted by Scott Appleton:
[B]Jyotsna,
If a method can or does throw a checked Exception, then it must either declare that it does via a "throws" clause in the method signature

or else it must place the statement which may throw a checked exception within a try-catch block

The code you posted does neither, so the compiler complains.[/B]

 
 
subject: Exception Problem