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