views:

68

answers:

1

When given an Exception object in Java, is there any way to get (or infer) the instances involved in the stack trace of this exception? I know that StackTraceElement contains information about the classes involved, but what about the actual instances?

In case you're wondering, I'd like to use this in a Thread.UncaughtExceptionHandler that displays error dialogs. The plan is to figure out which JFrame produced the unhandled exception and set that as the parent (this would affect positioning of the dialog, modality (which window it blocks) etc).

Thanks.

+1  A: 

AFAIK there's no standard API that supports that. My advise is as follows:

If you have a thread per JFrame just add a field to the Thread such that it will know what JFrame is associated with it such that it can manipulate it when an exception is caught.

Otherwise, if you have a central thread that carries out jobs dispatched by several JFrame objects, add a JFrame field to the job object such. When cataching an exception extract the JFrame from the Job object and you're good to go.

Itay
Is it possible to have a seperate thread for each JFrame ? AFAIK all GUI code in a Swing application is run through the Event Dispatch Thread.
sateesh
A GUI app can launch one (or more) Threads. These new threads should not do GUI-related operations. When they do need to do so they can use SwingUtilities.invokeLater(). I thought that in your scenario is about these "secondary" threads. If not, could you clarify?
Itay
I'm not the poster of the question, had posted a comment to know more about the answer you had posted. Thanks.
sateesh