001/*
002 * Copyright (c) 2007-2013, Stephen Colebourne & Michael Nascimento Santos
003 *
004 * All rights reserved.
005 *
006 * Redistribution and use in source and binary forms, with or without
007 * modification, are permitted provided that the following conditions are met:
008 *
009 *  * Redistributions of source code must retain the above copyright notice,
010 *    this list of conditions and the following disclaimer.
011 *
012 *  * Redistributions in binary form must reproduce the above copyright notice,
013 *    this list of conditions and the following disclaimer in the documentation
014 *    and/or other materials provided with the distribution.
015 *
016 *  * Neither the name of JSR-310 nor the names of its contributors
017 *    may be used to endorse or promote products derived from this software
018 *    without specific prior written permission.
019 *
020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031 */
032package org.threeten.bp.format;
033
034import java.io.IOException;
035
036import org.threeten.bp.DateTimeException;
037
038/**
039 * An exception thrown when an error occurs during printing.
040 * <p>
041 * This will be triggered by violations specific to printing or an IO exception.
042 *
043 * <h3>Specification for implementors</h3>
044 * This class is intended for use in a single thread.
045 */
046public class DateTimePrintException extends DateTimeException {
047
048    /**
049     * Serialization version.
050     */
051    private static final long serialVersionUID = 2263939197574006408L;
052
053    /**
054     * Constructs a new exception with the specified message.
055     *
056     * @param message  the message to use for this exception, may be null
057     */
058    public DateTimePrintException(String message) {
059        super(message, null);
060    }
061
062    /**
063     * Constructs a new exception with the specified message and cause.
064     *
065     * @param message  the message to use for this exception, may be null
066     * @param cause  the cause of the exception, may be null
067     */
068    public DateTimePrintException(String message, Throwable cause) {
069        super(message, cause);
070    }
071
072    //-----------------------------------------------------------------------
073    /**
074     * Checks if the cause of this exception was an IOException, and if so
075     * re-throws it
076     * <p>
077     * This method is useful if you call a printer with an open stream or
078     * writer and want to ensure that IOExceptions are not lost.
079     * <pre>
080     * try {
081     *   printer.print(writer, dateTime);
082     * } catch (DateTimePrintException ex) {
083     *   ex.rethrowIOException();
084     *   // if code reaches here exception was caused by date-time issues
085     * }
086     * </pre>
087     * Note that calling this method will re-throw the original IOException,
088     * causing this DateTimePrintException to be lost.
089     *
090     * @throws IOException if the cause of this exception is an IOException
091     */
092    public void rethrowIOException() throws IOException {
093        if (getCause() instanceof IOException) {
094            throw (IOException) getCause();
095        }
096    }
097
098}