aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Q? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception Q?" Watch "Exception Q?" New topic
Author

Exception Q?

Dian He
Greenhorn

Joined: Aug 03, 2001
Posts: 12
Why following code will cause a compilation error. Thanks.
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);
}
}
}
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Originally posted by Dian He:
Why following code will cause a compilation error. Thanks.
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);
}
}
}


The exception is not propagated. Any checked exception thrown
by throw statement, or indirectly calling the method that can invoke this exception, must deal with this either, handling it on the spot (ie try/catch) or just declare it as throws some
exception.
Whoever calls this method, ie the caller will handle it
HTH

[This message has been edited by Ragu Sivaraman (edited August 10, 2001).]
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370


Dian,
change ur first method signature from
static void throwMethod() to
static void throwMethod() to throws IllegalAccessException

------------------
azaman


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
Dian He
Greenhorn

Joined: Aug 03, 2001
Posts: 12

Thanks, clear now.
So two ways to fix this, one is mentioned as above, another is putting into a try/catch block in the throwMethod, is it?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Q?
 
Similar Threads
Compile Error
Exception doubt
Java Caps Mock
throw an Exception....
Do Static methods throw exceptions?