View Javadoc
1   package org.kuali.common.util.base;
2   
3   /**
4    * <p>
5    * Utility methods for creating {@code IllegalStateException's} and {@code IllegaArgumentException's} with richly formatted error messages.
6    * </p>
7    * 
8    * Example usage:
9    * 
10   * <pre>
11   * throw Exceptions.illegalArgument(&quot;port must be &gt;= %s and &lt;= %s&quot;, 0, 65535);
12   * </pre>
13   */
14  public class Exceptions {
15  
16  	public static IllegalStateException illegalState(Throwable cause) {
17  		return new IllegalStateException(cause);
18  	}
19  
20  	public static IllegalStateException illegalState(String msg) {
21  		return new IllegalStateException(msg);
22  	}
23  
24  	public static IllegalStateException illegalState(String msg, Object... args) {
25  		return new IllegalStateException(formattedMessage(msg, args));
26  	}
27  
28  	public static IllegalStateException illegalState(Throwable cause, String msg, Object... args) {
29  		return new IllegalStateException(formattedMessage(msg, args), cause);
30  	}
31  
32  	public static IllegalArgumentException illegalArgument(Throwable cause) {
33  		return new IllegalArgumentException(cause);
34  	}
35  
36  	public static IllegalArgumentException illegalArgument(String msg) {
37  		return new IllegalArgumentException(msg);
38  	}
39  
40  	public static IllegalArgumentException illegalArgument(String msg, Object... args) {
41  		return new IllegalArgumentException(formattedMessage(msg, args));
42  	}
43  
44  	public static IllegalArgumentException illegalArgument(Throwable cause, String msg, Object... args) {
45  		return new IllegalArgumentException(formattedMessage(msg, args), cause);
46  	}
47  
48  	protected static String formattedMessage(String msg, Object... args) {
49  		if (args == null || args.length == 0) {
50  			return msg;
51  		} else {
52  			return String.format(msg, args);
53  		}
54  	}
55  
56  }