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 */
017package org.apache.commons.math3.exception;
018
019import org.apache.commons.math3.exception.util.Localizable;
020import org.apache.commons.math3.exception.util.LocalizedFormats;
021import org.apache.commons.math3.exception.util.ExceptionContext;
022import org.apache.commons.math3.exception.util.ExceptionContextProvider;
023
024/**
025 * Base class for arithmetic exceptions.
026 * It is used for all the exceptions that have the semantics of the standard
027 * {@link ArithmeticException}, but must also provide a localized
028 * message.
029 *
030 * @since 3.0
031 * @version $Id: MathArithmeticException.java 1364378 2012-07-22 17:42:38Z tn $
032 */
033public class MathArithmeticException extends ArithmeticException
034    implements ExceptionContextProvider {
035    /** Serializable version Id. */
036    private static final long serialVersionUID = -6024911025449780478L;
037    /** Context. */
038    private final ExceptionContext context;
039
040    /**
041     * Default constructor.
042     */
043    public MathArithmeticException() {
044        context = new ExceptionContext(this);
045        context.addMessage(LocalizedFormats.ARITHMETIC_EXCEPTION);
046    }
047
048    /**
049     * Constructor with a specific message.
050     *
051     * @param pattern Message pattern providing the specific context of
052     * the error.
053     * @param args Arguments.
054     */
055    public MathArithmeticException(Localizable pattern,
056                                   Object ... args) {
057        context = new ExceptionContext(this);
058        context.addMessage(pattern, args);
059    }
060
061    /** {@inheritDoc} */
062    public ExceptionContext getContext() {
063        return context;
064    }
065
066    /** {@inheritDoc} */
067    @Override
068    public String getMessage() {
069        return context.getMessage();
070    }
071
072    /** {@inheritDoc} */
073    @Override
074    public String getLocalizedMessage() {
075        return context.getLocalizedMessage();
076    }
077}