qos-ch/slf4j

Apparently dead code in LoggerFactory

TWiStErRob opened this issue · 3 comments

While reading slf4j2 code to figure out how bindingsproviders work, I came across a line that confused me: catch(NoSuchFieldError) in LoggerFactory

private final static void versionSanityCheck() {
try {
String requested = PROVIDER.getRequestedApiVersion();
boolean match = false;
for (String aAPI_COMPATIBILITY_LIST : API_COMPATIBILITY_LIST) {
if (requested.startsWith(aAPI_COMPATIBILITY_LIST)) {
match = true;
}
}
if (!match) {
Util.report("The requested version " + requested + " by your slf4j provider is not compatible with "
+ Arrays.asList(API_COMPATIBILITY_LIST).toString());
Util.report("See " + VERSION_MISMATCH + " for further details.");
}
} catch (java.lang.NoSuchFieldError nsfe) {
// given our large user base and SLF4J's commitment to backward
// compatibility, we cannot cry here. Only for implementations
// which willingly declare a REQUESTED_API_VERSION field do we
// emit compatibility warnings.
} catch (Throwable e) {
// we should never reach here
Util.report("Unexpected problem occurred during version sanity check", e);
}
}

I was wondering why the comment references REQUESTED_API_VERSION, while there's no such thing in the try.

I found the change that invalidated that catch, and I'm still baffled as to why Java does not complain about a checked exception being not used I see why javac didn't alert: it's an Error, not checked!.

ceki commented

Hi @TWiStErRob

I think the NoSuchFieldError can no longer occur and it makes no sense to check for it. As for the compiler not complaining, it is a good question but beyond the scope of SLF4J. ☺️

See edit: Error is not a checked exception.

ceki commented

Unreachable code removed in commit 5ccbe20