title: Global exception handling (non REST controller code)
body: <p>I was trying to handle global exception but its not working without method call from REST API.</p>
  <p>I have following code</p>
  <pre class="lang-java prettyprint-override"><code>@SpringBootApplication 
  public class LogReduceDemoApplication {
  
      public static void main(String[] args) throws Exception {
          SpringApplication.run(LogReduceDemoApplication.class, args);
          System.out.println(&quot;.......Started......&quot;);
          LogReduceDemoApplication.testException();
      }
  
      public static void testException() throws Exception {
          throw new Exception(&quot;testException...&quot;);
      }
  }
  </code></pre>
  <p>Exception Handler</p>
  <pre class="lang-java prettyprint-override"><code>@ControllerAdvice
  public class ExceptionHelper {
  
      static final Logger logger = LoggerFactory.getLogger(ExceptionHelper.class.getName());
  
      @ExceptionHandler(value = { NullPointerException.class,Exception.class })
      public ResponseEntity&lt;Object&gt; handleException(Exception ex) {
          System.out.println(&quot;Inside handleException...&quot;);
          String msg=&quot;ex=&quot;+ex+&quot;, ex.getMessage()=&quot;+ex.getMessage();
          System.out.println(&quot;Exception Msg: &quot;+ msg);
          return new ResponseEntity&lt;Object&gt;(msg, HttpStatus.BAD_REQUEST);
      }
  }
  </code></pre>
  <p>When I am calling LogReduceDemoApplication.testException() method from REST controller then it is triggering exception handler. But when there is a call to the same method using main() function its not triggering Exception handler instead it is printing all exception details.</p>
  <p>How to use exception handler for the method call from main function(not from REST controller)?</p>