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.juneau; 018 019import static org.apache.juneau.common.utils.StringUtils.*; 020 021import java.text.*; 022 023import org.apache.juneau.common.utils.*; 024 025/** 026 * Subclass of non-runtime exceptions that take in a message and zero or more arguments. 027 * 028 * <h5 class='section'>See Also:</h5><ul> 029 030 * </ul> 031 * 032 * @serial exclude 033 */ 034public abstract class BasicException extends Exception { 035 036 private static final long serialVersionUID = 1L; 037 038 /** 039 * Constructor. 040 * 041 * @param message The {@link MessageFormat}-style message. 042 * @param args Optional {@link MessageFormat}-style arguments. 043 */ 044 public BasicException(String message, Object...args) { 045 super(format(message, args)); 046 } 047 048 /** 049 * Constructor. 050 * 051 * @param causedBy The cause of this exception. 052 * @param message The {@link MessageFormat}-style message. 053 * @param args Optional {@link MessageFormat}-style arguments. 054 */ 055 public BasicException(Throwable causedBy, String message, Object...args) { 056 this(message, args); 057 initCause(causedBy); 058 } 059 060 /** 061 * Constructor. 062 * 063 * @param causedBy The cause of this exception. 064 */ 065 public BasicException(Throwable causedBy) { 066 this(causedBy, causedBy.getLocalizedMessage()); 067 } 068 069 /** 070 * Same as {@link #getCause()} but searches the throwable chain for an exception of the specified type. 071 * 072 * @param c The throwable type to search for. 073 * @param <T> The throwable type to search for. 074 * @return The exception, or <jk>null</jk> if not found. 075 */ 076 public <T extends Throwable> T getCause(Class<T> c) { 077 return ThrowableUtils.getCause(c, this); 078 } 079 080 /** 081 * Returns the caused-by exception if there is one. 082 * 083 * @return The caused-by exception if there is one, or this exception if there isn't. 084 */ 085 public Throwable unwrap() { 086 Throwable t = getCause(); 087 return t == null ? this : t; 088 } 089}