brimborium
brimborium

Reputation: 9512

Globally catching Exception

I am working on a large scale application. I carefully try to handle all exceptions (and prevent them beforehand if possible). But as the application is large scale, sometimes some exceptions are thrown. When working in my development environment (i.e. starting the application from eclipse), I can see where the application occured, go there, and see what I can do about it. The problem is that - while developing - I have other people already "sort-of-test-working" with the application (inside the company). They give me a lot of feedback, which I like. Now if an exception is thrown in their application, there is no way for me to tell where this exception was created.

I have seen applications that show uncatched exceptions to the user over a dialog. While I surely don't want this to happen in the customer version, it might be of great help if I could do that in the debug version. Is this possible?

In short: Is it possible to catch all thrown and uncatched exceptions of a multithreaded Java application?

EDIT: I was asked, how the code is run. I create a runnable jar in eclipse containing all the libraries and class files. Then I pack this jar into an executable using Launch4j. This executable is then used to run the application (it is packed into a setup.exe together with the ressource files using Innosetup, but I guess this is not so important).

Codewise, I have a ThreadManager that starts all neccessary classes (including gui), but does not neccessarily keep a handle to all those classes.

Upvotes: 2

Views: 274

Answers (3)

Alexei Kaigorodov
Alexei Kaigorodov

Reputation: 13525

So you need each thread to catch all Throwable's (not just Exceptions) before dying and to dump them in a log.

Catching can be done in 2 ways:

a) override run() with

try {
   super.run()
 } catch(Throwable ex) {
   ex.printStackTrace(logWriter);
 }

b) use Thread.setDefaultUncaughtExceptionHandler()

If you use ThreadManager, then create your own ThreadFactory which creates threads which log exceptions in one way or another.

Upvotes: 0

Felix Reckers
Felix Reckers

Reputation: 1292

You should have a log where all exceptions are written. Furthermore, if you are using log4j, you could use the org.apache.log4j.net.SMTPAppender which will send you an email if an error was logged. This way you get an email every time an exception occurs in the application.

Upvotes: 0

alaster
alaster

Reputation: 4171

Use Thread.setDefaultUncaughtExceptionHandler() to handle all uncaught Exceptions.

See this

Upvotes: 3

Related Questions