ThrowableExtendedStackTraceRenderer catches Exception but not Error
Description
There is a loadClass method in the ThrowableExtendedStackTraceRenderer.
private static Class<?> loadClass(final ClassLoader loader, final String className) {
for (final ClassLoadingStrategy strategy : CLASS_LOADING_STRATEGIES) {
try {
final Class<?> clazz = strategy.run(loader, className);
if (clazz != null) {
return clazz;
}
} catch (final Exception ignored) {
// Do nothing
}
}
return null;
}
Which when unable to lookup the given class, should do nothing. But Classloaders throw NoClassDefFoundError which is an Error and thus not caught. This breaks log4j2, causing our app to log a beautify 'log4j2 cannot log something exception` and not the actual message+stacktrace it should log!
Configuration
Version: 2.25.3
Operating system: Windows, unrelated to this.
JDK: 21 and 25, also unrelated.
Logs
2026-01-21T13:27:17.646390600Z main ERROR An exception occurred processing Appender application-log-appender
org.apache.logging.log4j.core.appender.AppenderLoggingException: java.lang.NoClassDefFoundError: org/frankframework/core/IPushingListener
at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:164)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:133)
at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:124)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:88)
at org.apache.logging.log4j.core.appender.routing.RoutingAppender.append(RoutingAppender.java:256)
at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:160)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:133)
at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:124)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:88)
at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:711)
at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:669)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:645)
at org.apache.logging.log4j.core.config.LoggerConfig.logParent(LoggerConfig.java:702)
at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:671)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:645)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:589)
at org.apache.logging.log4j.core.config.DefaultReliabilityStrategy.log(DefaultReliabilityStrategy.java:73)
at org.apache.logging.log4j.core.Logger.log(Logger.java:187)
at org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(AbstractLogger.java:2970)
at org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(AbstractLogger.java:2922)
at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2904)
at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:2648)
at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:2587)
at org.apache.logging.log4j.spi.AbstractLogger.error(AbstractLogger.java:824)
.. many many more
Reproduction
Very difficult, requires custom classloaders. https://github.com/frankframework/frankframework/issues/10312
@nielsm5, would you be interested in contributing this fix along with a test?
@nielsm5, would you be interested in contributing this fix along with a test?
I tried but while testing the ThrowableExtendedStackTraceRenderer uses ~[?:?].
I'm also not sure if it's easy to test this. It requires custom classloading..
Hi @nielsm5 @vy , I'd like to work on this issue.
Expected fix: Change the catch block from Exception to Throwable:
} catch (final Throwable ignored) { // Changed from Exception
// Do nothing
}
This will properly handle NoClassDefFoundError and prevent the AppenderLoggingException.
I'll include a test case for custom classloaders throwing NoClassDefFoundError.
May I submit a PR for this?
@chanani, I grant you all the rights in the universe to submit a PR fixing the issue.
P.S. You don't need any permission from anyone to submit fixes (real fixes!) to Log4j. Make sure that ./mvnw verify is happy and there is an associated test + changelog entry.
I tried but while testing the
ThrowableExtendedStackTraceRendereruses~[?:?]. I'm also not sure if it's easy to test this. It requires custom classloading..
@nielsm5, maybe using reflection to manipulate CLASS_LOADING_STRATEGIES and having an entry there which throws an Error?