How can I get the line number where the exception was thrown using Thread.UncaughtExceptionHandler?

前端 未结 2 937
太阳男子
太阳男子 2020-12-14 06:49

When I use a try-catch block to catch an exception, I can get the line number where the exception was thrown by calling e.getStackTrace().

Like this:

2条回答
  •  情书的邮戳
    2020-12-14 07:11

    Try

    e.getStackTrace()[0].getLineNumber();
    

    The first element in the stack trace is the most recently called method (the top of the call stack).

    There are also methods to get the class, method and file name.

    If the e you get in the UncaughtExceptionHandler does not work well for you (because it wraps the real exception), you can try e.getCause() and look at that stack trace.

提交回复
热议问题