title: Why is "java.lang.OutOfMemoryError: Java heap space" not caught?
body: <p>I have a thread in a Java web application that causes a <strong>java.lang.OutOfMemoryError: Java heap space</strong> exception, but the try/catch block does not catch the error.</p>
  
  <p><strong>Sample code:</strong></p>
  
  <pre><code>private void doSomeWork()
  {
       try
       {
           processData();  //Causes OutOfMemoryError
           System.out.println("This line does not execute");
       }
       catch (Exception e)
       {
           System.out.println("Exception.  This line does not execute.");
           //Log error
       }
       finally
       {
           System.out.println("finally.  This line does execute");
           System.out.println("Thread name: " + Thread.currentThread().getName());
  
       }
  }
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre>
  finally.  This line does execute 
  Thread name: _Worker-8
  Exception in thread "_Worker-8" java.lang.OutOfMemoryError: Java heap space
  ...</pre>
  
  <p><strong>Background:</strong></p>
  
  <p>I recently took over this Java project and I'm trying to get up to speed with Java and this project.  I'm a C# developer, so I'm not yet familiar with this project or Java.
  I know I can fix the error using the -Xmx setting, but I'm interested in catching this error so I can log it.  The error is not showing up in any of the logs files, and the output is being shown in the console in debug mode in Eclipse.</p>