001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.mail;
019
020import java.io.PrintStream;
021import java.io.PrintWriter;
022
023/**
024 * Exception thrown when a checked error occurs in commons-email.
025 * <p>
026 * Adapted from FunctorException in Commons Collections.
027 * <p>
028 * Emulation support for nested exceptions has been removed in {@code Email 1.3},
029 * supported by JDK &ge; 1.4.
030 *
031 * @since 1.0
032 */
033public class EmailException
034        extends Exception
035{
036    /** Serializable version identifier. */
037    private static final long serialVersionUID = 5550674499282474616L;
038
039    /**
040     * Constructs a new <code>EmailException</code> with no
041     * detail message.
042     */
043    public EmailException()
044    {
045        super();
046    }
047
048    /**
049     * Constructs a new <code>EmailException</code> with specified
050     * detail message.
051     *
052     * @param msg  the error message.
053     */
054    public EmailException(final String msg)
055    {
056        super(msg);
057    }
058
059    /**
060     * Constructs a new <code>EmailException</code> with specified
061     * nested <code>Throwable</code> root cause.
062     *
063     * @param rootCause  the exception or error that caused this exception
064     *                   to be thrown.
065     */
066    public EmailException(final Throwable rootCause)
067    {
068        super(rootCause);
069    }
070
071    /**
072     * Constructs a new <code>EmailException</code> with specified
073     * detail message and nested <code>Throwable</code> root cause.
074     *
075     * @param msg  the error message.
076     * @param rootCause  the exception or error that caused this exception
077     *                   to be thrown.
078     */
079    public EmailException(final String msg, final Throwable rootCause)
080    {
081        super(msg, rootCause);
082    }
083
084    /**
085     * Prints the stack trace of this exception to the standard error stream.
086     */
087    @Override
088    public void printStackTrace()
089    {
090        printStackTrace(System.err);
091    }
092
093    /**
094     * Prints the stack trace of this exception to the specified stream.
095     *
096     * @param out  the <code>PrintStream</code> to use for output
097     */
098    @Override
099    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}