title: Java: Wrap code with a try catch block function?
body: <p>I have several functions where I am casting an object to an explicit type.</p>
  
  <p>The problem is, when this object is created, the type may not match the type I am casting to. Somewhere along the line there are some conversions that take place. However, it is possible the code never reaches these conversions. The fact that it doesn't is not an issue.</p>
  
  <p>So when the code reaches the point where the casting will occur, all I need to do is put the part into a try catch block. In fact I don't even need to handle the exception in any special way.</p>
  
  <p>But I have a lot of these functions. I would like to know if there is some way to wrap lines of code with a try catch block without actually writing out the try catch block. If I can call some function some how that will do it automatically.</p>
  
  <pre><code>try 
  { 
      // cast something 
  } 
  catch( ClassCastException e ) 
  { 
  }
  </code></pre>
  
  <p>Instead I want to call a method that will put the try catch block around <code>// cast something</code>. Is there any way to do something like this?</p>
  
  <p>Note: I don't do anything when I catch the ClassCastException. It is okay if I can't cast the object correctly, but I need to catch the exception so the code execution won't jump to some other place.</p>