I have a lot of code with try
/catch
and in almost all cases I take the same action. What I want to do is simplify my error handling by creating something where I can just say
ManagedExceptionEnvironment() {
// The code that might throw exceptions
}
And then inside of ManagedExceptionEnvironment
it has the shared error handling logic.
My initial thought on this was to have ManagedExceptionEnvironment
's constructor take a Runnable
, but if you put logic that might throw an exception inside an anonymous Runnable
's run
method then it still complains about not implementing try
/catch
even if the container you're passing it to would have taken care of it.
I suppose another option is to only handle the errors at the highest level and have them keep getting thrown up another level, though that seems rather risky.
Edit: I guess one option here (and I don't know if this is a thing in Java) is some kind of macro?
Edit 2: Just did some cursory reading on using the C pre-processor to inject macros into Java src and that is really horrifying. So macros are out as a solution.