Add extra information to uncaught exception

2019-08-19 00:19发布

问题:

In Google.Play there is a statistic about uncaught exceptions. I would like to know is there any way to add some extra information to exception, so it will be shown in Google.Play (cause third party error reporting services can't be used).

I know about Thread.setUncaughtExceptionHandler and I suppose I can write something like

public void uncaughtException (Thread thread, Throwable ex) {
    throw new CustomException(ex, "extraData");
}

with my custom exception message for example.

But are there any other ways to achieve this?

回答1:

Do you mean?

public class CustomException extends Exception
{
    public CustomException(Throwable cause, String message)
    {
        super(message, cause);
    }
}


回答2:

You could use ACRA. In addition to providing a lot more information than the Google Play crash reports, it also allows you to easily add custom data to the reports.



回答3:

I'd say you are missing the point here. In order to add information to an uncaught exception, you would need to catch the exception. Once you know where and how to catch the exception, the right approach is to handle the exception correctly so that no interruption to the user experience is caused.

Uncaught exceptions occur because you have overlooked an error condition and do not handle an error condition. Handle the error and there will be no uncaught exception to worry about.

Knowing possible error conditions is fundamental to writing well working programs. If you keep on cranking out code oblivious to errors and status codes you will create a mess that will crash on uncaught exceptions often. Handling exceptions and errors takes a lot of time and code but it is simply a part of programming that you can't ignore.