Where do unhandled exceptions go?

P

pek

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 ;)
 
A

Arne Vajhøj

pek said:
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
 
P

pek

pek said:
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/)
 
R

Roedy Green

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.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top