Coverage Report - org.apache.commons.math.MathException

Classes in this File Line Coverage Branch Coverage Complexity
MathException
83% 
100% 
1.125

 1  
 /*
 2  
  * Copyright 2003-2004 The Apache Software Foundation.
 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  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.commons.math;
 17  
 
 18  
 import java.io.PrintStream;
 19  
 import java.io.PrintWriter;
 20  
 
 21  
 
 22  
 /**
 23  
 * Base class for commons-math checked exceptions.
 24  
 * <p>
 25  
 * Supports nesting, emulating JDK 1.4 behavior if necessary.  
 26  
 * <p>
 27  
 * Adapted from {@link org.apache.commons.collections.FunctorException}.
 28  
 * 
 29  
 * @version $Revision$ $Date: 2005-02-26 05:11:52 -0800 (Sat, 26 Feb 2005) $
 30  
 */
 31  
 public class MathException extends Exception {
 32  
     
 33  
     /** Serializable version identifier */
 34  
     static final long serialVersionUID = -8594613561393443827L;
 35  
     
 36  
     /**
 37  
      * Does JDK support nested exceptions?
 38  
      */
 39  
     private static final boolean JDK_SUPPORTS_NESTED;
 40  
     
 41  
     static {
 42  18
         boolean flag = false;
 43  
         try {
 44  36
             Throwable.class.getDeclaredMethod("getCause", new Class[0]);
 45  18
             flag = true;
 46  0
         } catch (NoSuchMethodException ex) {
 47  0
             flag = false;
 48  18
         }
 49  18
         JDK_SUPPORTS_NESTED = flag;
 50  18
     }
 51  
     
 52  
     /**
 53  
      * Root cause of the exception
 54  
      */
 55  
     private final Throwable rootCause;
 56  
     
 57  
     /**
 58  
      * Constructs a new <code>MathException</code> with no
 59  
      * detail message.
 60  
      */
 61  
     public MathException() {
 62  2
         super();
 63  2
         this.rootCause = null;
 64  2
     }
 65  
     
 66  
     /**
 67  
      * Constructs a new <code>MathException</code> with specified
 68  
      * detail message.
 69  
      *
 70  
      * @param msg  the error message.
 71  
      */
 72  
     public MathException(String msg) {
 73  6
         super(msg);
 74  6
         this.rootCause = null;
 75  6
     }
 76  
     
 77  
     /**
 78  
      * Constructs a new <code>MathException</code> with specified
 79  
      * nested <code>Throwable</code> root cause.
 80  
      *
 81  
      * @param rootCause  the exception or error that caused this exception
 82  
      *                   to be thrown.
 83  
      */
 84  
     public MathException(Throwable rootCause) {
 85  2
         super((rootCause == null ? null : rootCause.getMessage()));
 86  2
         this.rootCause = rootCause;
 87  2
     }
 88  
     
 89  
     /**
 90  
      * Constructs a new <code>MathException</code> with specified
 91  
      * detail message and nested <code>Throwable</code> root cause.
 92  
      *
 93  
      * @param msg  the error message.
 94  
      * @param rootCause  the exception or error that caused this exception
 95  
      *                   to be thrown.
 96  
      */
 97  
     public MathException(String msg, Throwable rootCause) {
 98  42
         super(msg);
 99  42
         this.rootCause = rootCause;
 100  42
     }
 101  
     
 102  
     /**
 103  
      * Gets the cause of this throwable.
 104  
      * 
 105  
      * @return  the cause of this throwable, or <code>null</code>
 106  
      */
 107  
     public Throwable getCause() {
 108  46
         return rootCause;
 109  
     }
 110  
     
 111  
     /**
 112  
      * Prints the stack trace of this exception to the standard error stream.
 113  
      */
 114  
     public void printStackTrace() {
 115  0
         printStackTrace(System.err);
 116  0
     }
 117  
     
 118  
     /**
 119  
      * Prints the stack trace of this exception to the specified stream.
 120  
      *
 121  
      * @param out  the <code>PrintStream</code> to use for output
 122  
      */
 123  
     public void printStackTrace(PrintStream out) {
 124  6
         synchronized (out) {
 125  6
             PrintWriter pw = new PrintWriter(out, false);
 126  6
             printStackTrace(pw);
 127  
             // Flush the PrintWriter before it's GC'ed.
 128  6
             pw.flush();
 129  6
         }
 130  6
     }
 131  
     
 132  
     /**
 133  
      * Prints the stack trace of this exception to the specified writer.
 134  
      *
 135  
      * @param out  the <code>PrintWriter</code> to use for output
 136  
      */
 137  
     public void printStackTrace(PrintWriter out) {
 138  8
         synchronized (out) {
 139  8
             super.printStackTrace(out);
 140  8
             if (rootCause != null && JDK_SUPPORTS_NESTED == false) {
 141  0
                 out.print("Caused by: ");
 142  0
                 rootCause.printStackTrace(out);
 143  
             }
 144  8
         }
 145  8
     }
 146  
 
 147  
 }