jaulib v1.3.0
Jau Support Library (C++, Java, ..)
ExceptionUtils.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 Gothel Software e.K.
4 * Copyright (c) 2014 Gothel Software e.K.
5 * Copyright (c) 2014 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26package org.jau.lang;
27
28import java.io.PrintStream;
29
30/**
31 * @since 0.3.0
32 */
33public class ExceptionUtils {
34 public static void dumpStack(final PrintStream out) {
35 dumpStack(out, 1, -1);
36 }
37 public static void dumpStack(final PrintStream out, final int skip, final int depth) {
38 dumpStack(out, new Exception(""), skip+1, depth);
39 }
40 public static void dumpStack(final PrintStream out, final Throwable t, final int skip, final int depth) {
41 dumpStack(out, t.getStackTrace(), skip, depth);
42 }
43 public static void dumpStack(final PrintStream out, final StackTraceElement[] stack, final int skip, final int depth) {
44 if( null == stack ) {
45 return;
46 }
47 final int maxDepth;
48 if( 0 > depth ) {
49 maxDepth = stack.length;
50 } else {
51 maxDepth = Math.min(depth+skip, stack.length);
52 }
53 for(int i=skip; i<maxDepth; i++) {
54 out.println(" ["+i+"]: "+stack[i]);
55 }
56 }
57
58 /**
59 * Interface allowing {@link Throwable} specializations to provide their custom stack trace presentation.
60 * @since 0.3.0
61 */
62 public static interface CustomStackTrace {
63 /**
64 * Prints this {@link Throwable} as a cause to the output {@link PrintStream} {@code s},
65 * not iterating over all inner causes!
66 * @param s output stream
67 * @param causeStr the cause title
68 * @param causeIdx the cause index over all causes known by caller
69 * @param stackDepth the maximum depth for stack entries, or {@code -1} for all
70 * @since 0.3.0
71 */
72 void printCauseStack(final PrintStream s, final String causeStr, final int causeIdx, final int stackDepth);
73 /**
74 * Custom {@code printStackTrace} method, similar to {@link Throwable#printStackTrace(PrintStream, int, int)}.
75 * @param s output stream
76 * @param causeDepth the maximum depth for causes, or {@code -1} for all
77 * @param stackDepth the maximum depth for stack entries, or {@code -1} for all
78 */
79 void printStackTrace(final PrintStream s, final int causeDepth, final int stackDepth);
80 }
81
82 /**
83 * Prints the given {@link Throwable} cause to the output {@link PrintStream} {@code s}.
84 * @param s output stream
85 * @param causeStr the cause title
86 * @param cause the {@link Throwable} cause for output
87 * @param causeIdx the cause index over all causes known by caller
88 * @param causeDepth the maximum depth for causes, or {@code -1} for all
89 * @param stackDepth the maximum depth for stack entries, or {@code -1} for all
90 * @since 0.3.0
91 */
92 public static int printCause(final PrintStream s, final String causeStr, Throwable cause, final int causeIdx, final int causeDepth, final int stackDepth) {
93 int i=causeIdx;
94 for(; null != cause && ( -1 == causeDepth || i < causeDepth ); cause = cause.getCause()) {
95 if( cause instanceof CustomStackTrace ) {
96 ((CustomStackTrace)cause).printCauseStack(s, causeStr, i, stackDepth);
97 } else {
98 s.println(causeStr+"["+i+"] by "+cause.getClass().getSimpleName()+": "+cause.getMessage()+" on thread "+Thread.currentThread().getName());
99 dumpStack(s, cause.getStackTrace(), 0, stackDepth);
100 }
101 i++;
102 }
103 return i;
104 }
105
106 /**
107 * Prints the given {@link Throwable} to the output {@link PrintStream} {@code s}.
108 * @param s output stream
109 * @param t the {@link Throwable} for output
110 * @param causeDepth the maximum depth for causes, or {@code -1} for all
111 * @param stackDepth the maximum depth for stack entries, or {@code -1} for all
112 * @since 0.3.0
113 */
114 public static void printStackTrace(final PrintStream s, final Throwable t, final int causeDepth, final int stackDepth) {
115 if( t instanceof CustomStackTrace ) {
116 ((CustomStackTrace)t).printStackTrace(s, causeDepth, stackDepth);
117 } else {
118 s.println(t.getClass().getSimpleName()+": "+t.getMessage()+" on thread "+Thread.currentThread().getName());
119 dumpStack(s, t.getStackTrace(), 0, stackDepth);
120 printCause(s, "Caused", t.getCause(), 0, causeDepth, stackDepth);
121 }
122 }
123
124 /**
125 * Dumps a {@link Throwable} to {@link System.err} in a decorating message including the current thread name,
126 * and its {@link #dumpStack(PrintStream, StackTraceElement[], int, int) stack trace}.
127 * <p>
128 * Implementation will iterate through all {@link Throwable#getCause() causes}.
129 * </p>
130 * @param additionalDescr additional text placed before the {@link Throwable} details.
131 * @param t the {@link Throwable} for output
132 */
133 public static void dumpThrowable(final String additionalDescr, final Throwable t) {
134 dumpThrowable(additionalDescr, t, -1, -1);
135 }
136 /**
137 * Dumps a {@link Throwable} to {@link System.err} in a decorating message including the current thread name,
138 * and its {@link #dumpStack(PrintStream, StackTraceElement[], int, int) stack trace}.
139 * <p>
140 * Implementation will iterate through all {@link Throwable#getCause() causes}.
141 * </p>
142 * @param additionalDescr additional text placed before the {@link Throwable} details.
143 * @param t the {@link Throwable} for output
144 * @param causeDepth the maximum depth for causes, or {@code -1} for all
145 * @param stackDepth the maximum depth for stack entries, or {@code -1} for all
146 * @since 0.3.0
147 */
148 public static void dumpThrowable(final String additionalDescr, final Throwable t, final int causeDepth, final int stackDepth) {
149 System.err.print("Caught "+additionalDescr+" ");
150 printStackTrace(System.err, t, causeDepth, stackDepth);
151 }
152}
static void dumpThrowable(final String additionalDescr, final Throwable t, final int causeDepth, final int stackDepth)
Dumps a Throwable to System.err in a decorating message including the current thread name,...
static void dumpStack(final PrintStream out, final int skip, final int depth)
static int printCause(final PrintStream s, final String causeStr, Throwable cause, final int causeIdx, final int causeDepth, final int stackDepth)
Prints the given Throwable cause to the output PrintStream s.
static void dumpStack(final PrintStream out)
static void dumpStack(final PrintStream out, final StackTraceElement[] stack, final int skip, final int depth)
static void dumpThrowable(final String additionalDescr, final Throwable t)
Dumps a Throwable to System.err in a decorating message including the current thread name,...
static void dumpStack(final PrintStream out, final Throwable t, final int skip, final int depth)
static void printStackTrace(final PrintStream s, final Throwable t, final int causeDepth, final int stackDepth)
Prints the given Throwable to the output PrintStream s.
Interface allowing Throwable specializations to provide their custom stack trace presentation.
void printCauseStack(final PrintStream s, final String causeStr, final int causeIdx, final int stackDepth)
Prints this Throwable as a cause to the output PrintStream s, not iterating over all inner causes!
void printStackTrace(final PrintStream s, final int causeDepth, final int stackDepth)
Custom printStackTrace method, similar to Throwable#printStackTrace(PrintStream, int,...