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 * Adapted from FunctorException in Commons Collections. 27 * <p> 28 * Emulation support for nested exceptions has been removed in {@code Email 1.3}, 29 * supported by JDK ≥ 1.4. 30 * 31 * @since 1.0 32 */ 33 public class EmailException 34 extends Exception 35 { 36 /** Serializable version identifier. */ 37 private static final long serialVersionUID = 5550674499282474616L; 38 39 /** 40 * Constructs a new <code>EmailException</code> with no 41 * detail message. 42 */ 43 public EmailException() 44 { 45 super(); 46 } 47 48 /** 49 * Constructs a new <code>EmailException</code> with specified 50 * detail message. 51 * 52 * @param msg the error message. 53 */ 54 public EmailException(final String msg) 55 { 56 super(msg); 57 } 58 59 /** 60 * Constructs a new <code>EmailException</code> with specified 61 * nested <code>Throwable</code> root cause. 62 * 63 * @param rootCause the exception or error that caused this exception 64 * to be thrown. 65 */ 66 public EmailException(final Throwable rootCause) 67 { 68 super(rootCause); 69 } 70 71 /** 72 * Constructs a new <code>EmailException</code> with specified 73 * detail message and nested <code>Throwable</code> root cause. 74 * 75 * @param msg the error message. 76 * @param rootCause the exception or error that caused this exception 77 * to be thrown. 78 */ 79 public EmailException(final String msg, final Throwable rootCause) 80 { 81 super(msg, rootCause); 82 } 83 84 /** 85 * Prints the stack trace of this exception to the standard error stream. 86 */ 87 @Override 88 public void printStackTrace() 89 { 90 printStackTrace(System.err); 91 } 92 93 /** 94 * Prints the stack trace of this exception to the specified stream. 95 * 96 * @param out the <code>PrintStream</code> to use for output 97 */ 98 @Override 99 public void printStackTrace(final PrintStream out) 100 { 101 synchronized (out) 102 { 103 final PrintWriter pw = new PrintWriter(out, false); 104 printStackTrace(pw); 105 106 // Flush the PrintWriter before it's GC'ed. 107 pw.flush(); 108 } 109 } 110 111 /** 112 * Prints the stack trace of this exception to the specified writer. 113 * 114 * @param out the <code>PrintWriter</code> to use for output 115 */ 116 @Override 117 public void printStackTrace(final PrintWriter out) 118 { 119 synchronized (out) 120 { 121 super.printStackTrace(out); 122 } 123 } 124 }