001    /*
002     *  Copyright 2009-2013 Stephen Colebourne
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package org.joda.money.format;
017    
018    import java.io.IOException;
019    
020    /**
021     * Exception thrown during monetary formatting.
022     * <p>
023     * This exception makes no guarantees about immutability or thread-safety.
024     */
025    public class MoneyFormatException extends RuntimeException {
026    
027        /** Serialization lock. */
028        private static final long serialVersionUID = 87533576L;
029    
030        /**
031         * Constructor taking a message.
032         * 
033         * @param message  the message
034         */
035        public MoneyFormatException(String message) {
036            super(message);
037        }
038    
039        /**
040         * Constructor taking a message and cause.
041         * 
042         * @param message  the message
043         * @param cause  the exception cause
044         */
045        public MoneyFormatException(String message, Throwable cause) {
046            super(message, cause);
047        }
048    
049        //-----------------------------------------------------------------------
050        /**
051         * Checks if the cause of this exception was an IOException, and if so re-throws it
052         * <p>
053         * This method is useful if you call a printer with an open stream or
054         * writer and want to ensure that IOExceptions are not lost.
055         * <pre>
056         * try {
057         *   printer.print(writer, money);
058         * } catch (CalendricalFormatException ex) {
059         *   ex.rethrowIOException();
060         *   // if code reaches here exception was caused by issues other than IO
061         * }
062         * </pre>
063         * Note that calling this method will re-throw the original IOException,
064         * causing this MoneyFormatException to be lost.
065         *
066         * @throws IOException if the cause of this exception is an IOException
067         */
068        public void rethrowIOException() throws IOException {
069            if (getCause() instanceof IOException) {
070                throw (IOException) getCause();
071            }
072        }
073    
074    }