Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Handling *unchecked* exceptions. (http://www.velocityreviews.com/forums/t622575-handling-unchecked-exceptions.html)

RedGrittyBrick 06-27-2008 02:49 PM

Handling *unchecked* exceptions.
 
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

Daniel Pitts 06-27-2008 03:02 PM

Re: Handling *unchecked* exceptions.
 
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.setDefaultUncaughtExceptionHandle r

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

RedGrittyBrick 06-27-2008 04:17 PM

Re: Handling *unchecked* exceptions.
 
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


All times are GMT. The time now is 11:10 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.