title: How to get an exception message in a String variable in java?
body: <p>I need to process an <em>exception</em> message when any exception is caught in Java.</p>
  
  <p>I am working on a Database Connection class. When I give wrong details like username, password, host name, sid, etc, the control goes to <em>catch</em> block and gives an error. I would like to get this error message on the JSP side and redirect to the same page with that error message. However, when I get the error message in Java, it always takes a null value.</p>
  
  <p>My code example is here.</p>
  
  <pre><code>String errorMessage = null;
  try{
  // CODE Where Exception occure
  }catch(SQLException se){
      errorMessage = se.getMessage();
  }catch(Exception e){
      System. out.println("In Exception block.");
      errorMessage = e.getMessage();
  }finally{
      System.out.println(errorMessage);
  }
  </code></pre>
  
  <p>It will go to Exception block but the errorMessage is null. </p>