001 /*
002 * Copyright 2001-2006 Geert Bevin <gbevin[remove] at uwyn dot com>
003 * Distributed under the terms of either:
004 * - the common development and distribution license (CDDL), v1.0; or
005 * - the GNU Lesser General Public License, v2.1 or later
006 * $Id: ExceptionUtils.java 3108 2006-03-13 18:03:00Z gbevin $
007 */
008 package com.uwyn.jhighlight.tools;
009
010 import java.io.PrintWriter;
011 import java.io.StringWriter;
012
013 /**
014 * Collection of utility methods to work with exceptions.
015 *
016 * @author Geert Bevin (gbevin[remove] at uwyn dot com)
017 * @version $Revision: 3108 $
018 * @since 1.0
019 */
020 public abstract class ExceptionUtils
021 {
022 private ExceptionUtils()
023 {
024 }
025
026 /**
027 * Obtains the entire stracktrace of an exception and converts it into a
028 * string.
029 *
030 * @param exception the exception whose stacktrace has to be converted
031 * @return the stracktrace, converted into a string
032 * @since 1.0
033 */
034 public static String getExceptionStackTrace(Throwable exception)
035 {
036 if (null == exception) throw new IllegalArgumentException("exception can't be null;");
037
038 String stack_trace = null;
039
040 StringWriter string_writer = new StringWriter();
041 PrintWriter print_writer = new PrintWriter(string_writer);
042
043 exception.printStackTrace(print_writer);
044
045 stack_trace = string_writer.getBuffer().toString();
046
047 print_writer.close();
048
049 try
050 {
051 string_writer.close();
052 }
053 // JDK 1.2.2 compatibility
054 catch (Throwable e2)
055 {
056 }
057
058 return stack_trace;
059 }
060 }
061