hej
as i was creating a bunch of applications lately, i found it extremly annoying that i was never able to see the cause of an exception once it occured in my application.
this is why i slabbed together i piece of code which does exactly that, poping up a window once an exception occured. the nice thing is, that the application is actually able to continue after the error occured.
maybe you find it useful. actually i think it would be kind of nice if this was more integrated into processing. but maybe that s just me.
Code:
/**
* this sketch shows how to catch exceptions thrown by processing and display them in a popup window.
*/
void setup() {
size(320, 240);
}
/**
* throw the content of your 'draw' method in here.
*/
void drawSecure() {
background(255);
line(0, 0, mouseX, mouseY);
/* create an error */
if (mousePressed) {
String myString = null;
myString.length();
}
}
/**
* this 'draw' method to catches all exceptions.
*/
void draw() {
try {
drawSecure();
}
catch (Exception ex) {
handleError(ex, ex.getStackTrace());
}
}
/**
* this method handles the error.
* it pops up a dialog and continues the application if selected.
* @param myStackTrace StackTraceElement[]
*/
void handleError(Exception ex, StackTraceElement[] myStackTrace) {
StringBuffer myMessage = new StringBuffer();
for (int i = 0; i < min(30, myStackTrace.length); i++) {
myMessage.append(myStackTrace[i]);
myMessage.append('\n');
}
int myDecision = javax.swing.JOptionPane.showOptionDialog(this,
myMessage + "\nContinue?",
ex.toString(),
//"Processing Runtime Error",
javax.swing.JOptionPane.OK_CANCEL_OPTION,
javax.swing.JOptionPane.ERROR_MESSAGE,
null,
null,
null);
if (myDecision != 0) {
System.exit( -1); // this is a bit rude. hmm.
}
}