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.internal.StringUtils.*;
016
017import java.text.*;
018
019import org.apache.juneau.internal.*;
020
021/**
022 * Subclass of non-runtime exceptions that take in a message and zero or more arguments.
023 */
024public class BasicException extends Exception {
025
026   private static final long serialVersionUID = 1L;
027
028   /**
029    * Constructor.
030    *
031    * @param message The {@link MessageFormat}-style message.
032    * @param args Optional {@link MessageFormat}-style arguments.
033    */
034   public BasicException(String message, Object...args) {
035      super(format(message, args));
036   }
037
038   /**
039    * Constructor.
040    *
041    * @param causedBy The cause of this exception.
042    * @param message The {@link MessageFormat}-style message.
043    * @param args Optional {@link MessageFormat}-style arguments.
044    */
045   public BasicException(Throwable causedBy, String message, Object...args) {
046      this(message, args);
047      initCause(causedBy);
048   }
049
050   /**
051    * Constructor.
052    *
053    * @param causedBy The cause of this exception.
054    */
055   public BasicException(Throwable causedBy) {
056      this(causedBy, causedBy.getLocalizedMessage());
057   }
058
059   /**
060    * Same as {@link #getCause()} but searches the throwable chain for an exception of the specified type.
061    *
062    * @param c The throwable type to search for.
063    * @param <T> The throwable type to search for.
064    * @return The exception, or <jk>null</jk> if not found.
065    */
066   public <T extends Throwable> T getCause(Class<T> c) {
067      return ThrowableUtils.getCause(c, this);
068   }
069}