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.commons; 018 019import static org.apache.juneau.commons.utils.Shorts.*; 020 021/** 022 * Subclass of runtime exceptions that take in a message and zero or more arguments. 023 * 024 * 025 * @serial exclude 026 */ 027public class BasicRuntimeException extends RuntimeException { 028 private static final long serialVersionUID = 1L; 029 @SuppressWarnings({ 030 "java:S1104", // Field reassigned via setMessage() method, cannot be final 031 "java:S1165" // Cannot be final; reassigned by setMessage() on mutable exception instances 032 }) 033 String message; 034 035 /** 036 * Constructor. 037 * 038 * @param message The {@link String#format(String, Object...) String.format}-style message (<c>%s</c> placeholders). 039 * @param args Optional {@link String#format(String, Object...) String.format}-style arguments. 040 */ 041 public BasicRuntimeException(String message, Object...args) { 042 super(f(message, args)); 043 } 044 045 /** 046 * Constructor. 047 * 048 * @param cause The cause of this exception. Can be <jk>null</jk>. 049 */ 050 public BasicRuntimeException(Throwable cause) { 051 super(cause); 052 } 053 054 /** 055 * Constructor. 056 * 057 * @param cause The cause of this exception. Can be <jk>null</jk>. 058 * @param message The {@link String#format(String, Object...) String.format}-style message (<c>%s</c> placeholders). 059 * @param args Optional {@link String#format(String, Object...) String.format}-style arguments. 060 */ 061 public BasicRuntimeException(Throwable cause, String message, Object...args) { 062 super(f(message, args), cause); 063 } 064 065 @Override /* Overridden from Throwable */ 066 public String getMessage() { 067 return nn(message) ? message : super.getMessage(); 068 } 069 070 /** 071 * Sets the detail message on this exception. 072 * 073 * @param message The message. 074 * @param args The message args. 075 * @return This object. 076 */ 077 public BasicRuntimeException setMessage(String message, Object...args) { 078 this.message = f(message, args); 079 return this; 080 } 081}