01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: ExceptionUtils.java 3784 2007-06-11 16:44:35Z gbevin $
07: */
08: package com.uwyn.rife.tools;
09:
10: import java.io.PrintWriter;
11: import java.io.StringWriter;
12:
13: public abstract class ExceptionUtils {
14: public static String getExceptionStackTrace(Throwable exception) {
15: if (null == exception)
16: throw new IllegalArgumentException(
17: "exception can't be null;");
18:
19: String stack_trace = null;
20:
21: StringWriter string_writer = new StringWriter();
22: PrintWriter print_writer = new PrintWriter(string_writer);
23:
24: exception.printStackTrace(print_writer);
25:
26: stack_trace = string_writer.getBuffer().toString();
27:
28: print_writer.close();
29:
30: try {
31: string_writer.close();
32: }
33: // JDK 1.2.2 compatibility
34: catch (Throwable e2) {
35: }
36:
37: return stack_trace;
38: }
39:
40: public static String getExceptionStackTraceMessages(
41: Throwable exception) {
42: if (null == exception)
43: throw new IllegalArgumentException(
44: "exception can't be null;");
45:
46: StringBuilder messages = new StringBuilder();
47: Throwable t = exception;
48: while (t != null) {
49: if (messages.length() > 0) {
50: messages.append("; ");
51: }
52: messages.append(StringUtils.replace(t.getMessage(), "\n",
53: ""));
54:
55: if (t == t.getCause()) {
56: break;
57: }
58: t = t.getCause();
59: }
60:
61: return messages.toString();
62: }
63: }
|