Marcin Krzysiak
Marcin Krzysiak

Reputation: 249

How do you log an uncaught exception in Java?

My client is testing my beta phase Java application on a data that he cannot provide me with (privacy reasons). Although I implemented the handling of most exceptions, application crashes on rare occasions.

Can I log the stacktrace of an uncaught exception to a file so that tester can provide me with it(e.g. NullPointerException)?

Upvotes: 0

Views: 112

Answers (3)

Grzegorz Oledzki
Grzegorz Oledzki

Reputation: 24271

This is exactly the thing what UncaughtExceptionHandler concept is about.

Basically you are able to register a piece of code to be executed when the exception reaches the top of the stack.

Upvotes: 0

Thilo
Thilo

Reputation: 262684

If the main thread crashes, the JVM will print a stacktrace automatically.

For other threads, you can use setUncaughtExceptionHandler.

Upvotes: 0

dsgriffin
dsgriffin

Reputation: 68616

Set a Thread.UncaughtExceptionHandler that prints to the specified file.

Upvotes: 3

Related Questions