We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Catching Exceptions (Read 1002 times)
Catching Exceptions
Aug 18th, 2006, 5:34pm
 
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.
}
}
Page Index Toggle Pages: 1