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 runtime exceptions that take in a message and zero or more arguments.
023 */
024public class BasicRuntimeException extends RuntimeException {
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 BasicRuntimeException(String message, Object...args) {
035      super(format(message, args));
036   }
037
038   /**
039    * Constructor.
040    *
041    * @param cause The cause of this exception.
042    * @param message The {@link MessageFormat}-style message.
043    * @param args Optional {@link MessageFormat}-style arguments.
044    */
045   public BasicRuntimeException(Throwable cause, String message, Object...args) {
046      this(getMessage(cause, message, null), args);
047      initCause(cause);
048   }
049
050   /**
051    * Finds the message.
052    *
053    * @param cause The cause.
054    * @param msg The message.
055    * @param def The default value if both above are <jk>null</jk>.
056    * @return The resolved message.
057    */
058   protected static final String getMessage(Throwable cause, String msg, String def) {
059      if (msg != null)
060         return msg;
061      if (cause != null)
062         return cause.getMessage();
063      return def;
064   }
065
066   /**
067    * Same as {@link #getCause()} but searches the throwable chain for an exception of the specified type.
068    *
069    * @param c The throwable type to search for.
070    * @param <T> The throwable type to search for.
071    * @return The exception, or <jk>null</jk> if not found.
072    */
073   public <T extends Throwable> T getCause(Class<T> c) {
074      return ThrowableUtils.getCause(c, this);
075   }
076
077   // <FluentSetters>
078
079   // </FluentSetters>
080}