Handling *unchecked* exceptions.

Discussion in 'Java' started by RedGrittyBrick, Jun 27, 2008.

  1. I could use some advice.

    I'm used to throwing, catching, logging and recovering from (or not)
    *checked* exceptions.

    However, since I am not perfect, I may release a program that contains a
    bug that causes it to throw an unchecked exception, under some
    circumstance that I haven't tested for.

    Often these unchecked exceptions are not fatal to my application (they
    don't cause it to exit). I'd ideally like for my program to attempt to
    log that exception and attempt to display an error message to the user.

    Here's a silly example
    -----------------------------------------------------------------------
    public class ExceptionTest implements ActionListener {

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    new ExceptionTest();
    }
    });
    }

    JFrame f;

    ExceptionTest() {
    JButton b = new JButton("Go");
    b.addActionListener(this);

    JPanel p = new JPanel();
    p.add(b);

    f = new JFrame("Exception Handling");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(p);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
    JOptionPane.showMessageDialog(f, new FooPanel(null),
    "Title", JOptionPane.PLAIN_MESSAGE);
    }

    class FooPanel extends JPanel {
    FooPanel(Bar bar) {
    JTextField t = new JTextField(20);
    t.setText(bar.getName()); // NPE
    add(new JLabel("Bar: "));
    add(t);
    }
    }

    class Bar {
    private String name;

    Bar(String name) {
    this.name = name;
    }

    public String getName() {
    return name;
    }
    }

    }
    -----------------------------------------------------------------------

    If you click the button, basically the end-use sees nothing happen. Some
    messages are written by the JVM to STDERR. I guess this is being
    written by some class (perhaps by java.awt.EventDispatchThread.run() as
    that is last in the stackTrace).

    However, as this is a GUI app, the user doesn't see STDERR and nothing
    gets logged. This is unhelpful when the end-user reports that the button
    "does nothing".

    Note that the above program doesn't exit, and if the app had other
    functionality, the user could happily continue to use that other
    functionality.

    I could wrap the contents of actionPerformed() in "try {} catch
    (Exception e) {}" and attempt some logging and error dialogues. But it
    doesn't feel quite right to me. "Exception" seems overly broad?

    --
    RGB
     
    RedGrittyBrick, Jun 27, 2008
    #1
    1. Advertising

  2. RedGrittyBrick

    Daniel Pitts Guest

    RedGrittyBrick wrote:
    > I could use some advice.
    >
    > I'm used to throwing, catching, logging and recovering from (or not)
    > *checked* exceptions.
    >
    > However, since I am not perfect, I may release a program that contains a
    > bug that causes it to throw an unchecked exception, under some
    > circumstance that I haven't tested for.
    >
    > Often these unchecked exceptions are not fatal to my application (they
    > don't cause it to exit). I'd ideally like for my program to attempt to
    > log that exception and attempt to display an error message to the user.
    >

    [snip code]
    Look at java.lang.Thread.setDefaultUncaughtExceptionHandler

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Jun 27, 2008
    #2
    1. Advertising

  3. RGB> [How to catch unchecked exception so as to log and warn GUI user?]

    Daniel> Thread.setDefaultUncaughtExceptionHandler()
    Larry> Thread.setUncaughtExceptionHandler()


    I also pondered
    System.setErr(new PrintStream(new FileOutputStream(logFile)));

    But I think Thread.setDefaultUncaughtExceptionHandler() looks best.

    Thanks Daniel, Larry.

    --
    RGB
     
    RedGrittyBrick, Jun 27, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wendy S
    Replies:
    0
    Views:
    1,822
    Wendy S
    Feb 18, 2004
  2. Ahmed Moustafa
    Replies:
    5
    Views:
    30,042
    Chris Smith
    Jul 14, 2004
  3. exquisitus
    Replies:
    4
    Views:
    8,352
    Patricia Shanahan
    May 6, 2005
  4. Lew
    Replies:
    15
    Views:
    765
  5. RVic
    Replies:
    19
    Views:
    1,400
Loading...

Share This Page