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 act开发者_如何学JAVAual 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.
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.
精彩评论