catching an unhandled exception

D

Daniel

Hi all!
I have a game that seems to be running fine, but now that I want to
distribute this I would like to catch any unhandled exception, you
know the ones that just print a stack-trace to the console window. I
want to get that stack-trace to a file and pop-up a dialog to the user
informing them that something went very wrong and then close the
program.

Anyway I've read and looked on the net and there seems like there is
no good way to do this (am I right?)
At present I have written my own printstream that when it gets invoked
creates an exception and then counts the ammount of elements and write
the message to file, and when the countdown (the initial count done
only once) reaces zero it pops up a message and quits.
I of course install this as stdErr with the appropirate method calls

This feels like a rather ugly solution so I wonder if anyone has any
better ideas on how to handle this?

/Daniel
 
R

Rogan Dawes

Daniel said:
Hi all!
I have a game that seems to be running fine, but now that I want to
distribute this I would like to catch any unhandled exception, you
know the ones that just print a stack-trace to the console window. I
want to get that stack-trace to a file and pop-up a dialog to the user
informing them that something went very wrong and then close the
program.

Anyway I've read and looked on the net and there seems like there is
no good way to do this (am I right?)
At present I have written my own printstream that when it gets invoked
creates an exception and then counts the ammount of elements and write
the message to file, and when the countdown (the initial count done
only once) reaces zero it pops up a message and quits.
I of course install this as stdErr with the appropirate method calls

This feels like a rather ugly solution so I wonder if anyone has any
better ideas on how to handle this?

/Daniel

It sounds like you want to catch all Throwable's that have not been
otherwise handled, in effect.

Probably the best way to do this is to install an exception handler (a
catch clause) in all threads that you use, that catches the Throwable
and does something with it.

e.g. if you create a Thread, you might do something like:

public void run() {
try {
// your existing code here
} catch (Throwable t) {
// write the throwable to a file
// pop up a dialog
// exit
}
}

You should do this for all threads, and all Runnables that you create,
so that the top-level exception handler does not receive the exception.

Rogan
 
S

Shane Petroff

I believe it was Chris Smith or Jon Skeet, who posted a solution to this
a couple of years ago.

In main() Create a new ThreadGroup, override

uncaughtException(Thread, Throwable),

then start your real processing in another thread created within this
ThreadGroup. Subsequent threads will also be assigned to this ThreadGroup.

In 1.5, I believe there is a setUncaughtExceptionHandler on Thread.

The real question is what do you mean by: "you know the ones that just
print a stack-trace to the console window"? If you're handling
exceptions by merely printing stack traces, there is nothing but
training and rework which can solve the problem.
 
R

Raymond Martineau

It sounds like you want to catch all Throwable's that have not been
otherwise handled, in effect.

Trying to catch Throwable will also catch Errors (e.g. AWTError,
LinkageError, ThreadDeath, and VirutalMachineErrors.) Unless you want to
explicitly handle these things (the API says it is not recommended), it
might be better to use the following line instead:

} catch (Exception e) {
 
C

Chris Smith

Shane Petroff said:
I believe it was Chris Smith or Jon Skeet, who posted a solution to this
a couple of years ago.

It was probably me. Let me add one disclaimer:

If this is a GUI application, then it's not really specified which
thread group is used for the AWT event dispatch thread. My experience
is that it generally happens in the same thread group as the first
access to a GUI component; but that's not a guarantee. There's also a
trick with EventQueue that can be used to try and catch AWT exceptions.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Ryan Stewart

Chris Smith said:
It was probably me. Let me add one disclaimer:

If this is a GUI application, then it's not really specified which
thread group is used for the AWT event dispatch thread. My experience
is that it generally happens in the same thread group as the first
access to a GUI component; but that's not a guarantee. There's also a
trick with EventQueue that can be used to try and catch AWT exceptions.
Although, particularly in a game, you want to minimize the code in your event
handlers anyway.
 
G

gimme_this_gimme_that

I think you want :

StringWriter sw = new StringWriter();
ex.printStackTrace(new PrintWriter(sw));
String theStackTrace = sw.toString();
 
L

Larry Barowski

Daniel said:
Hi all!
I have a game that seems to be running fine, but now that I want to
distribute this I would like to catch any unhandled exception, you
know the ones that just print a stack-trace to the console window. I
want to get that stack-trace to a file and pop-up a dialog to the user
informing them that something went very wrong and then close the
program.

Another method from those given that will handle
uncaught exceptions on the EDT for Sun and IBM
JDKs since 1.2, and probably most others, is:

System.setProperty("sun.awt.exception.handler",
SomeClass.class.getName());

where SomeClass has a no-args constructor and
a method: public void handle(Throwable t);

If you're interested in actually fixing the problems,
and you have access to a web server where you
can put CGI programs, you can install a CGI
program that just dumps files (with increasing index
numbers). Then you can use a URLConnection
to upload the stack dump - with user permission,
of course. A much higher percentage of users
will click on a "Send Error Report" button than
will go through the trouble to email a file to you.
 
D

Daniel

thank you all for your input. The way it seems is that there is no
neat and nice way to do this. but you must do some "patching"
I have not had the time to try any of the suggestions yet.
I found the posting about the property for the exception handler for
the EDT very interesting, and must say this was what I was looking
for, but for the entire application, all threads.
In my little world it would seem that
System.registerExceptionHandler(Handler h)
would be a nice feature.
and Handler is an interface that has the method
public void handle(Throwable t)
is there any reason this is a bad idea? or is it simply not done yet?
It would be really neat to have that, that would make the application
nicer to handle from a user perspective, and also from a programmers
perspective. Of course there may be some bad side effects from this
that I do not yet see.
again thanks for your input!
/Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top