Handling *unchecked* exceptions.

R

RedGrittyBrick

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?
 
D

Daniel Pitts

RedGrittyBrick said:
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
 
R

RedGrittyBrick

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top