001/** 002 * The MIT License (MIT) 003 * 004 * Copyright (c) 2015-2016 decimal4j (tools4j), Marco Terzer 005 * 006 * Permission is hereby granted, free of charge, to any person obtaining a copy 007 * of this software and associated documentation files (the "Software"), to deal 008 * in the Software without restriction, including without limitation the rights 009 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 010 * copies of the Software, and to permit persons to whom the Software is 011 * furnished to do so, subject to the following conditions: 012 * 013 * The above copyright notice and this permission notice shall be included in all 014 * copies or substantial portions of the Software. 015 * 016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 020 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 021 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 022 * SOFTWARE. 023 */ 024package org.decimal4j.arithmetic; 025 026import java.math.RoundingMode; 027 028/** 029 * Utility for exception conversion and re-throwing. 030 */ 031public final class Exceptions { 032 033 private static final String ROUNDING_NECESSARY = "Rounding necessary"; 034 035 /** 036 * Returns a new {@link ArithmeticException} with the given {@code message} 037 * and nested {@code cause}. 038 * 039 * @param message 040 * the exception message 041 * @param cause 042 * the causing exception 043 * @return an arithmetic exception with the given message and cause 044 */ 045 public static final ArithmeticException newArithmeticExceptionWithCause(String message, Exception cause) { 046 return (ArithmeticException) new ArithmeticException(message).initCause(cause); 047 } 048 049 /** 050 * Returns new {@link ArithmeticException} indicating that rounding was 051 * necessary when attempting to apply rounding with 052 * {@link RoundingMode#UNNECESSARY}. 053 * 054 * @return an arithmetic exception with the message "Rounding necessary" 055 */ 056 public static final ArithmeticException newRoundingNecessaryArithmeticException() { 057 return new ArithmeticException(ROUNDING_NECESSARY); 058 } 059 060 /** 061 * Rethrows the given arithmetic exception if its message equals 062 * "Rounding necessary". Otherwise the method does nothing. 063 * 064 * @param e 065 * the exception to rethrow if it is of the "Rounding necessary" 066 * type 067 * @throws ArithmeticException 068 * rethrows the given exception {@code e} if its message equals 069 * "Rounding necessary" 070 */ 071 public static final void rethrowIfRoundingNecessary(ArithmeticException e) { 072 if (ROUNDING_NECESSARY.equals(e.getMessage())) { 073 throw e; 074 } 075 } 076 077 // no instances 078 private Exceptions() { 079 super(); 080 } 081}