File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Designing with exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Designing with exception handling" Watch "Designing with exception handling" New topic

Designing with exception handling

Keith Jones
Ranch Hand

Joined: Oct 30, 2006
Posts: 105
Greetings all,

I have a tricky problem that I wonder if anyone has a solution for. I have lots of GUI classes and another class which implements ActionListener and has an actionPeformed method. Now what I wanted to do was have only 1 try catch block in my code. This try catch block I wanted to place in my main method. Problem is my actionPerformed method contains a call to a method that throws an exception. I don't want to catch the exception in the actionPerformed method (as that will break my design such that it is) and I can't put a throws clause in the header of the actionPerformed method as this would mean that I'm not actually implemented the ActionListener interface. Anyone have a solution to this?

SCJP 1.4
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

I don't know what to tell you other than that this "design" isn't workable. Exceptions propagate within a single thread only, and the GUI thread is different from the one in which main() lives.

Now, what you want, really, is centralized error reporting, and that is a good thing. There are many alternatives, but they all boil down to having one reporting method you can simply call from any catch block you write. That reporting method might throw up an error dialog, or log the error using log4j, or both, or something else altogether.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Designing with exception handling
It's not a secret anymore!