Exception Reporter

In the following code,

 public class SomeWidget {
 Text fileContent_;
 void setup() {
  ...
  dialog_.addSelectionListener(new SelectionListener() {
    public void widgetSelected(SelectionEvent e) {
        String fileName = dialog_.open();
        try {
           fileContent_.setText(readFileContents(fileName));
        }
        catch (IOException e) {
           throw new RuntimeException(e);
        }
    }
    ...
  });
 }
 readFileContents(String name) throws IOException {/*Some code*/}
 }
The readFileContents throws an IOException, which cannot be re-thrown because the SelectionListener is an interface which does not declare a throws clause in the method widgetSelected. Hence, it is not possible to let the user of SomeWidget handle the exception, because even if we throw a RuntimeException, it gets thrown in a Thread that is different from the one that uses SomeWidget.

One solution could be to use Publisher-Subscriber technique for error reporting.

For example:

 public class SomeWidget {
   Text fileContent_;

void setup() { ... dialog_.addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent e) { String fileName = dialog_.open(); try { fileContent_.setText(readFileContents(fileName)); } catch (IOException e) { reportException(e); } } ... }); }

readFileContents(String name) throws IOException {/*Some code*/}

addExceptionListener(ExceptionListener l) { excListeners_.add(l); }

reportException(Throwable t) { /* Iterate thro' excListeners_ and call exceptionOccurred() */ } }

public interface ExceptionListener { void exceptionOccurred(Throwable t); }

/* Clients of SomeWidget. */

... SomeWidget s = new SomeWidget(); ... s.addExceptionListener(new ExceptionListener() { void exceptionOccurred(Throwable t) { MessageDialog.showDialog(t.getMessage()); } }); ...

This technique allows for more than one handlers to the same exception, centralized error reporting etc.

-- VhIndukumar with Biresh


CategoryException


EditText of this page (last edited May 8, 2010) or FindPage with title or text search