Wednesday, August 01, 2007

I always wondered how to do that!

So, have you ever been writing code and new there was a probability an exception was going to get thrown. For whatever reason you want to do something with the stack trace kind of unusual, and you want more than just the exception.getMessage() in fact you want the whole stack trace.

Now I never claimed to be a rocket scientist. Some days I don't even claim to be clever. Today I took the time to figure out how to catch the exception and turn the complete stack trace into a String that you can do whatever you want to with. Here is the snippet I used.

catch (Exception e){
OutputStream os = new ByteArrayOutputStream();
PrintWriter writer= new PrintWriter(os);

String myMessage = new String(
//do something with the string

So, like I said it's not rocket science but it is something I always wondered how to do.


No comments: