OK, here's the problem space:
Lwjgl, a graphical library, has the ability to throw an unchecked LWJGLException
on certain methods - like Display.create()
. This becomes a problem when you have to off-load all work done by Display
onto a single thread using an ExecutorService
- that's what I'm doing, unfortunately, due to how Lwjgl works. ExecutorService.submit(Runnable)
does not allow you to throw unchecked exceptions. ExecutorService.submit(Callable<V>)
does, however, I still have to handle that exception.
My solution? I used the strategy pattern, like so.
private final ExecutorService executor;
private final ExceptionHandler exceptionHandler;
@Inject
public LwjglWindow(@Lwjgl ExecutorService executor, @Lwjgl ExceptionHandler exceptionHandler) {
this.executor = executor;
this.exceptionHandler = exceptionHandler;
}
public void start() {
executor.submit(() -> {
try {
Display.create();
} catch (LWJGLException ex) {
exceptionHandler.onException(ex);
}
});
}
Using dependency injection, I inject an instance of ExceptionHandler
to deal specifically with exceptions originating from Lwjgl. Should an exception occur from Lwjgl, well, then, this is called (from LwjglExceptionHandler
):
@Override
public void onException(Exception exception) {
logger.trace("lwjgl encountered an error", exception);
systemShutdownStrategy.shutdown();
}
systemShutdownStrategy
is an interface that is nothing more than a strategy pattern. This is injected into the instance of LwjglExceptionHandler
, so it could be a stub and do nothing or it could shut down your program (which is, as the name of the field suggests, the intent). There's always room to implement some user-friendly error notification, rather than closing quietly, but this is just here for demonstration purposes.
The reason behind my choices here is that if a LwjglException
occurs, the program is pretty much guaranteed to be unrecoverable. However, I'm concerned that I might be creating a code smell by the way I implement by start()
method in LwjglWindow
, given the side effects possible.
So, my question is, given that this is how I am handling my LwjglException
s, is it a code smell, and what alternatives would you suggest in that case? (Also, a general review of my conventions would be appreciated if you'd like, as obviously just getting back into Java after 2 years in the .NETsphere)