001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau; 014 015import static org.apache.juneau.common.internal.StringUtils.*; 016 017import java.text.*; 018 019import org.apache.juneau.common.internal.*; 020 021/** 022 * Subclass of non-runtime exceptions that take in a message and zero or more arguments. 023 * 024 * <h5 class='section'>See Also:</h5><ul> 025 026 * </ul> 027 * 028 * @serial exclude 029 */ 030public abstract class BasicException extends Exception { 031 032 private static final long serialVersionUID = 1L; 033 034 /** 035 * Constructor. 036 * 037 * @param message The {@link MessageFormat}-style message. 038 * @param args Optional {@link MessageFormat}-style arguments. 039 */ 040 public BasicException(String message, Object...args) { 041 super(format(message, args)); 042 } 043 044 /** 045 * Constructor. 046 * 047 * @param causedBy The cause of this exception. 048 * @param message The {@link MessageFormat}-style message. 049 * @param args Optional {@link MessageFormat}-style arguments. 050 */ 051 public BasicException(Throwable causedBy, String message, Object...args) { 052 this(message, args); 053 initCause(causedBy); 054 } 055 056 /** 057 * Constructor. 058 * 059 * @param causedBy The cause of this exception. 060 */ 061 public BasicException(Throwable causedBy) { 062 this(causedBy, causedBy.getLocalizedMessage()); 063 } 064 065 /** 066 * Same as {@link #getCause()} but searches the throwable chain for an exception of the specified type. 067 * 068 * @param c The throwable type to search for. 069 * @param <T> The throwable type to search for. 070 * @return The exception, or <jk>null</jk> if not found. 071 */ 072 public <T extends Throwable> T getCause(Class<T> c) { 073 return ThrowableUtils.getCause(c, this); 074 } 075 076 /** 077 * Returns the caused-by exception if there is one. 078 * 079 * @return The caused-by exception if there is one, or this exception if there isn't. 080 */ 081 public Throwable unwrap() { 082 Throwable t = getCause(); 083 return t == null ? this : t; 084 } 085}