Interesting code: Wy JDBC database driver do not register correctly

Interesting code: Wy database driver do not register correctly

Today I came around the static method java.sql.DriverManager.loadInitialDrivers which is called in the static initializer of DriverManager. It handles the automatic JDBC driver registration, which depends highly on the java.util.ServiceLoader class.
In short the DriverManger searches the classpath for META-INF/services/java.sql.Driverresources to force the load of the classes defined in the found resources. This is done by the following code found in loadInitialDrivers

[code lang=java]
while(driversIterator.hasNext()) {;
} catch(Throwable t) {
// Do nothing

In the past this works in nearly every environment with all kind of JARs on the classpath. But today it fails
with a simple exception:

SQLException: No suitable driver found for …

It took me some time to find out why:
On the classpath a JAR shows up, which introduced a META-INF/services/java.sql.Driver containing a class, which static initializer throws a what every exception. The problem is: every exception which is thrown in the while will leave the loop — pending resource will not be evaluated and all pending JDBC driver will not get registered.

I found the following comment before this loop:

Load these drivers, so that they can be instantiated.
It may be the case that the driver class may not be there
i.e. there may be a packaged driver with the service class
as implementation of java.sql.Driver but the actual class
may be missing. In that case a java.util.ServiceConfigurationError
will be thrown at runtime by the VM trying to locate
and load the service.

Adding a try catch block to catch those runtime errors
if driver not available in classpath but it’s
packaged as service and that service is there in classpath.

Nice try, because in those cases no other driver will be registered, because the first which fails will exit the loop.

@JDK-Developer: Can we just put the try...catch into the loop
@JDBC-Driver-Developer: Please do not throw any exception during your static initializer.

JDK: 1.8.0_101 Mac