Is there a way to catch all exceptions? (to fix them)

Hi, somebody trying my app got a random crash after 15 minutes of playing around and the only thing it displays is the /very/ generic NULL exception. I want to fix the issue, but I don’t know where to start because it never happens on my computer. Does anybody know /exactly/ how to implement a way to catch all exceptions so that I could dump a log containing a sh*tload of variables and also, last but not least… the damn call stack lol… At least I would know on what line it crashed lol…

Thanks.

Are you sure you dont have a proper catch somewhere?

do at least ensure that everywhere is expection.printStackTrace()

Per default jme do log the full stacktrace to the console.

…But not on the compiled app! It doesn’t crash here, it crashes (rarely, but still SOMETIMES) over on my friend’s computer… therefore, I need some way to dump a log file of what happened, so that he can send it to me, or even better, it could upload it to my web server for me to instantly see what’s going on.

a) depending on the exception, it might not have a message so ‘pretty print’ style dialogs will just show “null”… and NullPointerException is a common one. That’s usually my guess when I see that… but I always include a message in any exceptions I throw myself. For Java exceptions, I think NPE is one of the only ones without a message.

b) you can capture exceptions at runtime and log them. You can configure the logging to write a log file even. For my stuff, I also roll the logs over at startup so that the last five hang around (this was trivial to do in log4j but JUL logging is the single worst logging API I’ve ever used and I’m not sure how to do it in that). I also log a bunch of app info at the same time and I’ve considered standardizing this into a utility (*).

Anyway, you can go a long way by implementing handleError in your application and dumping the exception + stack trace to a file. Then at least you will have it. You can do similar for background threads you have running though that’s trickier and depends on how you are running them. (Well, the easy way is to put a big try/catch in your run or call methods but it’s not entirely elegant.)

  • One reason I haven’t standardized it is because I had to go directly to the lwjgl libs to get the info I wanted because JME doesn’t provide access. I always meant to add the access so I could make it more generic.
@pspeed said: Anyway, you can go a long way by implementing handleError in your application and dumping the exception + stack trace to a file.

Please show me/us an example of how to actually implement this. It’s worth mentionning again that what I’m looking for is a way to catch all exceptions and stack trace them to a file, even if it means that the exception is in LWJGL core or JME3 core files.

Thx Paul for taking the time to actually pin point the solution so that I do it /correctly/ the first time and catch this M***** null exception once and for all :smiley:

? Writing a stack trace is not hard, really. So before I unnecessarily regurgitate file handling code, which part is it that you need help with exactly?

For me, I have a utility method that I call that dumps it to a file named “error-” + System.currentTimeMillis() + “.log”. Then I call that from handleError() and I call that from any other threads where I’m catching exceptions. Application.handleError() only sees the ones from the render thread.

Found the appropriate answer here: http://hub.jmonkeyengine.org/forum/topic/handling-of-errors/

Especially this post: http://hub.jmonkeyengine.org/forum/topic/handling-of-errors/#post-197699

Thx

For anybody coming here later on, here’s the code snippet to paste in the Main.java file to capture most (if not all?) exceptions occuring so that you can at least get a dump file that people can send you at runtime for you to debug the crash (please feel free to correct my code if it’s wrong)… Have fun with it! :smiley:

[java]
private FileWriter text_file_log_writer;

public String get_clock_time(){
DecimalFormat df = new DecimalFormat(“00”);
Calendar c = Calendar.getInstance();
return df.format(c.get(Calendar.HOUR)) + “:” + df.format(c.get(Calendar.MINUTE)) + “:” + df.format(c.get(Calendar.SECOND));
}

@Override
public void handleError(String errMsg, Throwable t){
String s = “”;
for(StackTraceElement e : t.getStackTrace()){
s += e.toString() + “\r\n”;
}
s = “EXCEPTION: " + t.getMessage() + “\r\n” + s;
System.out.println(s);
try {
// Create a new log file
File file = new File(“LOG.txt”);
file.createNewFile();
text_file_log_writer = new FileWriter(file);
text_file_log_writer.write(”[" + get_clock_time() + "] " + s + “\r\n”);
text_file_log_writer.flush();
text_file_log_writer.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
[/java]

1 Like