title: Create exception safe execution wrapper in Java
body: <p>I have a lot of code with <code>try</code>/<code>catch</code> 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</p>
  
  <pre><code>ManagedExceptionEnvironment() {
      // The code that might throw exceptions
  }
  </code></pre>
  
  <p>And then inside of <code>ManagedExceptionEnvironment</code> it has the shared error handling logic.</p>
  
  <p>My initial thought on this was to have <code>ManagedExceptionEnvironment</code>'s constructor take a <code>Runnable</code>, but if you put logic that might throw an exception inside an anonymous <code>Runnable</code>'s <code>run</code> method then it still complains about not implementing <code>try</code>/<code>catch</code> even if the container you're passing it to would have taken care of it. </p>
  
  <p>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. </p>
  
  <p>Edit: I guess one option here (and I don't know if this is a thing in Java) is some kind of macro?</p>
  
  <p>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.</p>