Where do unhandled exceptions go?

Discussion in 'Java' started by pek, Jun 21, 2008.

  1. pek

    pek Guest

    I'm trying to create a Main class (that includes the main method) and
    catch all uncaught exceptions. So my code started like this:

    public class Main {
    public static void main(String[] args) {
    try {
    start();
    } catch(Exception e) {
    handleException(e);
    }

    public static void handleException(Exception e) {
    // This will look for the instanceof an Exception and do some
    things. For any other type
    // it will display a simple dialog that will inform the user that
    there was an error
    // in the program and prompt him to exit or continue at his own
    risk
    logUnhandledException(e);
    }

    public static void logUnhandledException(Exception e) {
    // Log it
    }

    public static void start(String[] args) {
    // The code I would normally put in the main method
    }
    }

    This code works perfectly and catches and logs all the exceptions that
    where not caught by anything.. But things get complicated when I need
    to start threads.. For example, to load a frame I would do this:

    public static void start(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    try {
    new MyFrameClass().setVisible(true);
    }catch(Exception e) { handleException(e); }
    }
    });
    }

    This works OK.. So I'm thinking that it wouldn't be a trouble.. But
    what about this:

    public MyFrameClass() {
    JButton button = new JButton();
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ev) {
    throw new NullPointerException();
    }
    });
    add(button);
    }

    If the action of the button causes a runtime-exception, how the hell
    do I catch it? Of course, I could wrap the whole method in a try
    catch, but I'm wondering: where does the exception go? I suppose it is
    somewhere in the EDT. So, is there a way to "globally" catch
    exceptions that I didn't add a try catch block in the EDT?

    I'm not sure if I made myself clear but, as always, thank you in
    advanced ;)
     
    pek, Jun 21, 2008
    #1
    1. Advertising

  2. pek

    Arne Vajhøj Guest

    pek wrote:
    > This code works perfectly and catches and logs all the exceptions that
    > where not caught by anything.. But things get complicated when I need
    > to start threads.. For example, to load a frame I would do this:
    >
    > public static void start(String[] args) {
    > SwingUtilities.invokeLater(new Runnable() {
    > public void run() {
    > try {
    > new MyFrameClass().setVisible(true);
    > }catch(Exception e) { handleException(e); }
    > }
    > });
    > }
    >
    > This works OK.. So I'm thinking that it wouldn't be a trouble.. But
    > what about this:
    >
    > public MyFrameClass() {
    > JButton button = new JButton();
    > button.addActionListener(new ActionListener() {
    > public void actionPerformed(ActionEvent ev) {
    > throw new NullPointerException();
    > }
    > });
    > add(button);
    > }
    >
    > If the action of the button causes a runtime-exception, how the hell
    > do I catch it? Of course, I could wrap the whole method in a try
    > catch, but I'm wondering: where does the exception go? I suppose it is
    > somewhere in the EDT. So, is there a way to "globally" catch
    > exceptions that I didn't add a try catch block in the EDT?


    Maybe Thread.setDefaultUncaughtExceptionHandler can help you.

    Arne
     
    Arne Vajhøj, Jun 21, 2008
    #2
    1. Advertising

  3. pek

    pek Guest

    On Jun 21, 3:14 am, Arne Vajhøj <> wrote:
    > pek wrote:
    > > This code works perfectly and catches and logs all the exceptions that
    > > where not caught by anything.. But things get complicated when I need
    > > to start threads.. For example, to load a frame I would do this:

    >
    > > public static void start(String[] args) {
    > > SwingUtilities.invokeLater(new Runnable() {
    > > public void run() {
    > > try {
    > > new MyFrameClass().setVisible(true);
    > > }catch(Exception e) { handleException(e); }
    > > }
    > > });
    > > }

    >
    > > This works OK.. So I'm thinking that it wouldn't be a trouble.. But
    > > what about this:

    >
    > > public MyFrameClass() {
    > > JButton button = new JButton();
    > > button.addActionListener(new ActionListener() {
    > > public void actionPerformed(ActionEvent ev) {
    > > throw new NullPointerException();
    > > }
    > > });
    > > add(button);
    > > }

    >
    > > If the action of the button causes a runtime-exception, how the hell
    > > do I catch it? Of course, I could wrap the whole method in a try
    > > catch, but I'm wondering: where does the exception go? I suppose it is
    > > somewhere in the EDT. So, is there a way to "globally" catch
    > > exceptions that I didn't add a try catch block in the EDT?

    >
    > Maybe Thread.setDefaultUncaughtExceptionHandler can help you.
    >
    > Arne


    Holy #^#&^!!! That was exactly what I was looking for..!!! :D
    I can't thank you enough! A million thank you's (www.noob.us/humor/a-
    million-thank-yous/)
     
    pek, Jun 21, 2008
    #3
  4. pek

    Roedy Green Guest

    If you don't catch an exception in main, it percolates back the Java
    run time which will produce and error message and die. This is quite
    reasonable behaviour for exceptions that should never happen.

    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jun 21, 2008
    #4
  5. pek

    pek Guest

    On Jun 21, 6:07 am, Roedy Green <>
    wrote:
    > If you don't catch an exception in main, it percolates back the Java
    > run time which will produce and error message and die.  This is quite
    > reasonable behaviour for exceptions that should never happen.
    >
    > --
    >
    > Roedy Green Canadian Mind Products
    > The Java Glossaryhttp://mindprod.com


    Yes, but you don't want this to happen in production.. Better catch
    and log:
    http://stuffthathappens.com/blog/2007/10/07/programmers-notebook-uncaught-exception-handlers/
     
    pek, Jun 21, 2008
    #5
    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. terry

    Unhandled Exceptions & Framesets

    terry, Jul 2, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    431
    Bassel Tabbara [MSFT]
    Jul 3, 2003
  2. Replies:
    1
    Views:
    399
    Darren Kopp
    Mar 7, 2006
  3. Alan Silver

    How do I catch unhandled exceptions

    Alan Silver, Mar 20, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    1,732
    Alan Silver
    Mar 23, 2006
  4. Winista

    Unhandled Exceptions thrown

    Winista, Mar 22, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    401
    Winista
    Mar 22, 2006
  5. Warren Tang
    Replies:
    1
    Views:
    885
    Warren Tang
    Sep 23, 2008
Loading...

Share This Page