问题
I try to write my own loader-class which loads an encryted class.
Therefore I also override the contruction loader(ClassLoader paramClassLoader, File paramFile), which calls super(new URL[] { paramFile.toURI().toURL() }, paramClassLoader);.
The call ".toUrl()" can throw an MalformedURLException, so compiling the following code ...
public class loader extends URLClassLoader {
public static void main(String[] args)throws Exception{
Object localObject =
new loader(loader.class.getClassLoader(),
new File(loader.class.getProtectionDomain().getCodeSource()
.getLocation().getPath())
);
(...)
}
private loader(ClassLoader paramClassLoader, File paramFile){
super(new URL[] { paramFile.toURI().toURL() }, paramClassLoader);
if (paramClassLoader == null)
throw new IllegalArgumentException("Error loading class");
}
}
Error:
loader.java:123: error: unreported exception MalformedURLException; must be caught or declared to be thrown
super(new URL[] { paramFile.toURI().toURL() }, paramClassLoader);
How can I catch this exception? A try-catch-block is not possible because the "call to super must be first statement in constructor".
回答1:
Just add throws MalformedURLException to loader constructor and wrap code in main method with try catch block.
public class loader extends URLClassLoader {
public static void main(String[] args) throws Exception {
try {
Object localObject = new loader(loader.class.getClassLoader(),
new File(loader.class.getProtectionDomain().getCodeSource()
.getLocation().getPath()));
} catch (MalformedURLException e) {
// ..
}
}
private loader(ClassLoader paramClassLoader, File paramFile)
throws MalformedURLException {
super(new URL[] { paramFile.toURI().toURL() }, paramClassLoader);
if (paramClassLoader == null) {
throw new IllegalArgumentException("Error loading class");
}
}
}
回答2:
The exception isn't actually thrown by the superclass constructor; it's thrown (or at least declared to be potentially thrown) by URI.toURL(), which you're calling in your argument to the superclass constructor.
One option would be to write a static method to convert that exception into an unchecked one:
private static URL convertFileToURL(File file) {
try {
return file.toURI().toURL();
} catch (MalformedURLException e) {
throw new RuntimeException("Unable to convert file to URL", e);
}
}
Then:
private loader(ClassLoader paramClassLoader, File paramFile){
super(new URL[] { convertFileToURL(paramFile) }, paramClassLoader);
if (paramClassLoader == null)
throw new IllegalArgumentException("Error loading class");
}
That's assuming you view this as something which basically can't happen, or at least which you don't want callers to care about. I don't know enough about URI.toURL to know whether it's actually a concern with file-based URIs.
If callers should care, because it can happen in real life and they ought to deal with it (which I consider unlikely to be honest) you should just declare that your constructor could throw the exception.
As an aside, please rename your class to something more meaningful which follows Java naming conventions.
来源:https://stackoverflow.com/questions/17830004/java-catch-exception-from-super-constructor