Try / Catch not catching exception

I am trying to load a java class. I am using eclipse (luna) with 1.8.0_25 on yosemite macos.

I am using the following code to do this. It works until it reaches a class that has a dependency that is not contained inside the jar from which I load the classes. Not in the environment.

I am not interested in these classes, so then it can be ignored. So I put it in try / catch and just log it.

But when an exception is thrown, execution is canceled.

Exception (summarized):

Exception in thread "main" java.lang.NoClassDefFoundError: org/aspectj/runtime/internal/AroundClosure

Caused by: java.lang.ClassNotFoundException: org.aspectj.runtime.internal.AroundClosure

      

It rises on the exact line that is inside the inner try / catch block.

Why? Should the try / catch attempt log it and move on?

URLClassLoader ucl = null;
try {
    URL url = Utils.getURIFromPath(jarFilePath).toURL();
    URL[] urls = new URL[] { url };
    ucl = new URLClassLoader(urls);
    for (String tmp : classes) {
        String clazz = tmp.substring(0, tmp.indexOf(".class")).replaceAll("/", ".");
        try {
            ucl.loadClass(clazz);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
} catch (MalformedURLException e) {
    LOGGER.error(e.getMessage(), e);
} finally {
    if (ucl != null) {
        try {
            ucl.close();
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
}

      

+3


source to share


3 answers


NoClassDefFoundError

is an error, not Exception

, you should never try 1catch

error, as indicated here , catching it will not help you.

Check your classpath and fix it.



1 In some cases, you might want the catch

error and handle it accordingly, as pointed out in the comments, maybe "never" is a bit strong

+6


source


Ok then try this:

catch (Throwable e) {
LOGGER.error(e.getMessage(), e);
}

      



Although using the above code will handle both errors and exceptions for you (both of them range from Throwable

), but this is not the correct way to do it, only in special cases we handle errors with try-catch, errors we need to check our code and resolve them.

+2


source


It seems that one of the classes you are using, probably one of the classes you are loading with loadClass

, has a dependency on classes from AspectJ , but ClassLoader does not find AspectJ classes. Possible solution: make sure AspectJ is on the classpath when you do this.

It could also be that AspectJ is already on the classpath. If so, it's just that yours URLClassLoader

doesn't know your ClassPath. You might want to build URLClassLoader

with an ClassLoader

in-place parent like:

ucl = new URLClassLoader(urls, getClass().getClassLoader());

      

The reason the program doesn't just move is because you are catching Exception

. Hierarchy class Throwable

, class Exception extends Throwable

and class Error extends Throwable

therefore catch (Exception e)

will not catch Error

. You can use catch(Exception | NoClassDefFoundError e)

in this special case. Usually catching errors is not a good idea, but sometimes there are reasonable exceptions for errors such as NoClassDefFoundError

, OutOfMemoryError

or StackOverflowError

.

+1


source







All Articles