1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.mail; 19 20 import java.io.PrintStream; 21 import java.io.PrintWriter; 22 23 /** 24 * Exception thrown when a checked error occurs in commons-email. 25 * <p> 26 * Supports nesting, emulating JDK 1.4 behavior if necessary. 27 * <p> 28 * Adapted from FunctorException in Commons Collections. 29 * 30 * @author jakarta-commons 31 * @since 1.0 32 * @version $Id: EmailException.java 510812 2007-02-23 04:32:22Z dion $ 33 */ 34 public class EmailException 35 extends Exception 36 { 37 /** Serializable version identifier */ 38 static final long serialVersionUID = 5550674499282474616L; 39 40 /** 41 * Does JDK support nested exceptions? 42 */ 43 private static final boolean JDK_SUPPORTS_NESTED; 44 45 static 46 { 47 boolean flag = false; 48 49 try 50 { 51 Throwable.class.getDeclaredMethod("getCause", new Class[0]); 52 flag = true; 53 } 54 catch (NoSuchMethodException ex) 55 { 56 flag = false; 57 } 58 59 JDK_SUPPORTS_NESTED = flag; 60 } 61 62 /** 63 * Root cause of the exception 64 */ 65 private final Throwable rootCause; 66 67 /** 68 * Constructs a new <code>EmailException</code> with no 69 * detail message. 70 */ 71 public EmailException() 72 { 73 super(); 74 this.rootCause = null; 75 } 76 77 /** 78 * Constructs a new <code>EmailException</code> with specified 79 * detail message. 80 * 81 * @param msg the error message. 82 */ 83 public EmailException(String msg) 84 { 85 super(msg); 86 this.rootCause = null; 87 } 88 89 /** 90 * Constructs a new <code>EmailException</code> with specified 91 * nested <code>Throwable</code> root cause. 92 * 93 * @param rootCause the exception or error that caused this exception 94 * to be thrown. 95 */ 96 public EmailException(Throwable rootCause) 97 { 98 super((rootCause == null) ? null : rootCause.getMessage()); 99 this.rootCause = rootCause; 100 } 101 102 /** 103 * Constructs a new <code>EmailException</code> with specified 104 * detail message and nested <code>Throwable</code> root cause. 105 * 106 * @param msg the error message. 107 * @param rootCause the exception or error that caused this exception 108 * to be thrown. 109 */ 110 public EmailException(String msg, Throwable rootCause) 111 { 112 super(msg); 113 this.rootCause = rootCause; 114 } 115 116 /** 117 * Gets the cause of this throwable. 118 * 119 * @return the cause of this throwable, or <code>null</code> 120 */ 121 public Throwable getCause() 122 { 123 return rootCause; 124 } 125 126 /** 127 * Prints the stack trace of this exception to the standard error stream. 128 */ 129 public void printStackTrace() 130 { 131 printStackTrace(System.err); 132 } 133 134 /** 135 * Prints the stack trace of this exception to the specified stream. 136 * 137 * @param out the <code>PrintStream</code> to use for output 138 */ 139 public void printStackTrace(PrintStream out) 140 { 141 synchronized (out) 142 { 143 PrintWriter pw = new PrintWriter(out, false); 144 printStackTrace(pw); 145 146 // Flush the PrintWriter before it's GC'ed. 147 pw.flush(); 148 } 149 } 150 151 /** 152 * Prints the stack trace of this exception to the specified writer. 153 * 154 * @param out the <code>PrintWriter</code> to use for output 155 */ 156 public void printStackTrace(PrintWriter out) 157 { 158 synchronized (out) 159 { 160 super.printStackTrace(out); 161 162 if (!JDK_SUPPORTS_NESTED && (rootCause != null)) 163 { 164 out.print("Caused by: "); 165 rootCause.printStackTrace(out); 166 } 167 } 168 } 169 }