View Javadoc

1   package org.lsst.ccs.utilities.exc;
2   
3   /**
4    * This is to create a Bundle of linked Exception : a code may not stop at the first Exception it catches
5    * but store the Exception in reverse order and then , when finished, throw a Set of Exception.
6    * Typical code could look like this:
7    * <PRE>
8    *      ThrowableList listExc = null
9    *     for (Thing thing : things) {
10   *         try {
11   *
12   *         } catch(AnException exc) {
13   *              // or use a specific subclass of BundledException
14   *             listExc = new BundledException(exc, listExc)
15   *     }
16   *     if(listExc != null) {
17   *         throw  listExc.current() ;
18   *     }
19   * </PRE>
20   * Beware: do not confuse this with a "cause" (such as in getCause()).
21   * <P>
22   *     The interface <TT>ThrowableList</TT> may help create
23   *     list of throwables that do not necessarily extends BundledException.
24   *     (this could have been implemented through multiple decorators ...)
25   *
26   * @author bamade
27   */
28  // Date: 06/06/12
29  
30  public class BundledException extends Exception implements ThrowableList{
31      ThrowableList previousThrowable ;
32  
33      public BundledException(ThrowableList previousThrowable) {
34          this.previousThrowable = previousThrowable;
35      }
36  
37      public BundledException(String s, ThrowableList previousThrowable) {
38          super(s);
39          this.previousThrowable = previousThrowable;
40      }
41  
42      public BundledException(String s, Throwable throwable, ThrowableList previousThrowable) {
43          super(s, throwable);
44          this.previousThrowable = previousThrowable;
45      }
46  
47      public BundledException(Throwable throwable, ThrowableList previousThrowable) {
48          super(throwable);
49          this.previousThrowable = previousThrowable;
50      }
51  
52      public Throwable previous() {
53          return (Throwable) this.previousThrowable ;
54      }
55  
56      @Override
57      public Throwable current() {
58          return this;
59      }
60  
61      public String toString() {
62          return super.toString()+ "\n" + (previousThrowable!=null? previousThrowable.toString():"") ;
63      }
64  
65      public void setPreviousThrowable(ThrowableList previousThrowable) {
66          this.previousThrowable = previousThrowable;
67      }
68  }