Skip to main content
summaryrefslogtreecommitdiffstats
blob: dcb51283cde1a6361623d2799630206a5a13ed81 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*******************************************************************************
 * Copyright (c) 2009, 2010 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.internal;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Collection;

/**
 * Provide a way for multiple exceptions to be packaged and reported.
 */
public class CompositeException
	extends RuntimeException
{
	private final Throwable[] exceptions;
	private static final long serialVersionUID = 1L;


	/**
	 * The specified exceptions list must not be empty.
	 */
	public CompositeException(Collection<Throwable> exceptions) {
		this(exceptions.toArray(new Throwable[exceptions.size()]));
	}

	/**
	 * The specified exceptions list must not be empty.
	 */
	public CompositeException(Throwable[] exceptions) {
		// provide a list of the nested exceptions and
		// grab the first exception and make it the "cause"
		super(buildMessage(exceptions));
		this.exceptions = exceptions;
	}

	public Throwable[] getExceptions() {
		return this.exceptions;
	}

	private static String buildMessage(Throwable[] exceptions) {
		StringBuilder sb = new StringBuilder();
		sb.append(exceptions.length);
		sb.append(" exceptions: "); //$NON-NLS-1$
		sb.append('[');
		for (Throwable ex : exceptions) {
			sb.append(ex.getClass().getSimpleName());
			sb.append(", "); //$NON-NLS-1$
		}
		sb.setLength(sb.length() - 2);  // chop off trailing comma
		sb.append(']');
		return sb.toString();
	}

	@Override
	public void printStackTrace(PrintStream s) {
		synchronized (s) {
			s.println(this);
			for (StackTraceElement element : this.getStackTrace()) {
				s.print("\tat "); //$NON-NLS-1$
				s.println(element);
			}
			int i = 1;
			for (Throwable ex : this.exceptions) {
				s.print("Nested exception "); //$NON-NLS-1$
				s.print(i++);
				s.print(": "); //$NON-NLS-1$
				ex.printStackTrace(s);
			}
		}
	}

	@Override
	public void printStackTrace(PrintWriter s) {
		synchronized (s) {
			s.println(this);
			for (StackTraceElement element : this.getStackTrace()) {
				s.print("\tat "); //$NON-NLS-1$
				s.println(element);
			}
			int i = 1;
			for (Throwable ex : this.exceptions) {
				s.print("Nested exception "); //$NON-NLS-1$
				s.print(i++);
				s.print(": "); //$NON-NLS-1$
				ex.printStackTrace(s);
			}
		}
	}
}

Back to the top