LoginSignup
1
0

More than 3 years have passed since last update.

JavaのNullPointerExceptionのスタックトレースをログに落とす

Last updated at Posted at 2019-07-31

SpringBootにて
悪しきぬるぽ(真に悪しきはバグを作ったおまえ)が出たときいつも追えなくて困るので、スタックトレースを出すハンドラをControllerの下とかにくっつけておくことにしました。
デバッグに意外と役立つ。

@ExceptionHandler({NullPointerException.class})
public String handleRunTimeException(NullPointerException ex) throws IOException {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    ex.printStackTrace(pw);
    String stackTraceString = sw.toString();
    log.error(stackTraceString);
    sw.close();
    pw.close();
    return "redirect:error/500";
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0