1 // ======================================================================== 2 // Copyright 1999-2005 Mort Bay Consulting Pty. Ltd. 3 // ------------------------------------------------------------------------ 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 // ======================================================================== 14 15 package org.mortbay.util; 16 import java.io.PrintStream; 17 import java.io.PrintWriter; 18 import java.util.List; 19 20 21 /* ------------------------------------------------------------ */ 22 /** Wraps multiple exceptions. 23 * 24 * Allows multiple exceptions to be thrown as a single exception. 25 * 26 * @author Greg Wilkins (gregw) 27 */ 28 public class MultiException extends Exception 29 { 30 private Object nested; 31 32 /* ------------------------------------------------------------ */ 33 public MultiException() 34 { 35 super("Multiple exceptions"); 36 } 37 38 /* ------------------------------------------------------------ */ 39 public void add(Throwable e) 40 { 41 if (e instanceof MultiException) 42 { 43 MultiException me = (MultiException)e; 44 for (int i=0;i<LazyList.size(me.nested);i++) 45 nested=LazyList.add(nested,LazyList.get(me.nested,i)); 46 } 47 else 48 nested=LazyList.add(nested,e); 49 } 50 51 /* ------------------------------------------------------------ */ 52 public int size() 53 { 54 return LazyList.size(nested); 55 } 56 57 /* ------------------------------------------------------------ */ 58 public List getThrowables() 59 { 60 return LazyList.getList(nested); 61 } 62 63 /* ------------------------------------------------------------ */ 64 public Throwable getThrowable(int i) 65 { 66 return (Throwable) LazyList.get(nested,i); 67 } 68 69 /* ------------------------------------------------------------ */ 70 /** Throw a multiexception. 71 * If this multi exception is empty then no action is taken. If it 72 * contains a single exception that is thrown, otherwise the this 73 * multi exception is thrown. 74 * @exception Exception 75 */ 76 public void ifExceptionThrow() 77 throws Exception 78 { 79 switch (LazyList.size(nested)) 80 { 81 case 0: 82 break; 83 case 1: 84 Throwable th=(Throwable)LazyList.get(nested,0); 85 if (th instanceof Error) 86 throw (Error)th; 87 if (th instanceof Exception) 88 throw (Exception)th; 89 default: 90 throw this; 91 } 92 } 93 94 /* ------------------------------------------------------------ */ 95 /** Throw a Runtime exception. 96 * If this multi exception is empty then no action is taken. If it 97 * contains a single error or runtime exception that is thrown, otherwise the this 98 * multi exception is thrown, wrapped in a runtime exception. 99 * @exception Error If this exception contains exactly 1 {@link Error} 100 * @exception RuntimeException If this exception contains 1 {@link Throwable} but it is not an error, 101 * or it contains more than 1 {@link Throwable} of any type. 102 */ 103 public void ifExceptionThrowRuntime() 104 throws Error 105 { 106 switch (LazyList.size(nested)) 107 { 108 case 0: 109 break; 110 case 1: 111 Throwable th=(Throwable)LazyList.get(nested,0); 112 if (th instanceof Error) 113 throw (Error)th; 114 else if (th instanceof RuntimeException) 115 throw (RuntimeException)th; 116 else 117 throw new RuntimeException(th); 118 default: 119 throw new RuntimeException(this); 120 } 121 } 122 123 /* ------------------------------------------------------------ */ 124 /** Throw a multiexception. 125 * If this multi exception is empty then no action is taken. If it 126 * contains a any exceptions then this 127 * multi exception is thrown. 128 */ 129 public void ifExceptionThrowMulti() 130 throws MultiException 131 { 132 if (LazyList.size(nested)>0) 133 throw this; 134 } 135 136 /* ------------------------------------------------------------ */ 137 public String toString() 138 { 139 if (LazyList.size(nested)>0) 140 return "org.mortbay.util.MultiException"+ 141 LazyList.getList(nested); 142 return "org.mortbay.util.MultiException[]"; 143 } 144 145 /* ------------------------------------------------------------ */ 146 public void printStackTrace() 147 { 148 super.printStackTrace(); 149 for (int i=0;i<LazyList.size(nested);i++) 150 ((Throwable)LazyList.get(nested,i)).printStackTrace(); 151 } 152 153 154 /* ------------------------------------------------------------------------------- */ 155 /** 156 * @see java.lang.Throwable#printStackTrace(java.io.PrintStream) 157 */ 158 public void printStackTrace(PrintStream out) 159 { 160 super.printStackTrace(out); 161 for (int i=0;i<LazyList.size(nested);i++) 162 ((Throwable)LazyList.get(nested,i)).printStackTrace(out); 163 } 164 165 /* ------------------------------------------------------------------------------- */ 166 /** 167 * @see java.lang.Throwable#printStackTrace(java.io.PrintWriter) 168 */ 169 public void printStackTrace(PrintWriter out) 170 { 171 super.printStackTrace(out); 172 for (int i=0;i<LazyList.size(nested);i++) 173 ((Throwable)LazyList.get(nested,i)).printStackTrace(out); 174 } 175 176 }