1 /* 2 * Copyright (c) 2002-2007 by OpenSymphony 3 * All rights reserved. 4 */ 5 package com.opensymphony.xwork2.interceptor; 6 7 import java.io.IOException; 8 import java.io.PrintWriter; 9 import java.io.StringWriter; 10 import java.io.Serializable; 11 12 /** 13 * <!-- START SNIPPET: javadoc --> 14 * 15 * A simple wrapper around an exception, providing an easy way to print out the stack trace of the exception as well as 16 * a way to get a handle on the exception itself. 17 * 18 * <!-- END SNIPPET: javadoc --> 19 * 20 * @author Matthew E. Porter (matthew dot porter at metissian dot com) 21 */ 22 public class ExceptionHolder implements Serializable { 23 24 private Exception exception; 25 26 /** 27 * Holds the given exception 28 * 29 * @param exception the exception to hold. 30 */ 31 public ExceptionHolder(Exception exception) { 32 this.exception = exception; 33 } 34 35 /** 36 * Gets the holded exception 37 * 38 * @return the holded exception 39 */ 40 public Exception getException() { 41 return this.exception; 42 } 43 44 /** 45 * Gets the holded exception stacktrace using {@link Exception#printStackTrace()}. 46 * 47 * @return stacktrace 48 */ 49 public String getExceptionStack() { 50 String exceptionStack = null; 51 52 if (getException() != null) { 53 StringWriter sw = new StringWriter(); 54 PrintWriter pw = new PrintWriter(sw); 55 56 try { 57 getException().printStackTrace(pw); 58 exceptionStack = sw.toString(); 59 } 60 finally { 61 try { 62 sw.close(); 63 pw.close(); 64 } catch (IOException e) { 65 // ignore 66 } 67 } 68 } 69 70 return exceptionStack; 71 } 72 73 }