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 */
032public class MathArithmeticException extends ArithmeticException
033    implements ExceptionContextProvider {
034    /** Serializable version Id. */
035    private static final long serialVersionUID = -6024911025449780478L;
036    /** Context. */
037    private final ExceptionContext context;
038
039    /**
040     * Default constructor.
041     */
042    public MathArithmeticException() {
043        context = new ExceptionContext(this);
044        context.addMessage(LocalizedFormats.ARITHMETIC_EXCEPTION);
045    }
046
047    /**
048     * Constructor with a specific message.
049     *
050     * @param pattern Message pattern providing the specific context of
051     * the error.
052     * @param args Arguments.
053     */
054    public MathArithmeticException(Localizable pattern,
055                                   Object ... args) {
056        context = new ExceptionContext(this);
057        context.addMessage(pattern, args);
058    }
059
060    /** {@inheritDoc} */
061    public ExceptionContext getContext() {
062        return context;
063    }
064
065    /** {@inheritDoc} */
066    @Override
067    public String getMessage() {
068        return context.getMessage();
069    }
070
071    /** {@inheritDoc} */
072    @Override
073    public String getLocalizedMessage() {
074        return context.getLocalizedMessage();
075    }
076}