1 int i = 4; 2 int j = 5; 3 Preconditions.checkargument (I>j, "I should bigger than J, but I am%s and J is%s", i,j);
Exception in thread "main" java.lang.illegalargumentexception:i should bigger than J, but I am 4 and J is 5 at Co M.google.common.base.preconditions.checkargument (Preconditions.java:145) at Google.guava.GuavaDemo.main (Guavademo.java:) at sun.reflect.NativeMethodAccessorImpl.invoke0 ( Native Method) at Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:+) At Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:+) at Java.lang.reflect.Method.invoke (Method.java:497) at Com.intellij.rt.execution.application.AppMain.main (Appmain.java:140)
First put on the most commonly used checkargument, you can see that when the expression is not satisfied, the output error message can be defined by itself, and very clear.
The following is an explanation of the 3 parameters:
- No extra arguments. Any exceptions is thrown without error messages. No error messages
- An extra Object argument. Any exception are thrown with the error message object.tostring (). As long as the error message
- An extra String argument, with an arbitrary number of additional Object arguments. This behaves something like printf, but for GWT compatibility and efficiency, it is only allows %s indicators plus variable, Then check if the value is incorrect when you make an error
Others should be used very little, in the project ACK found only used checkargument. You don't have to explain.
Checkargument (Boolean) |
Checks the boolean is true. Use the validating arguments to methods. |
IllegalArgumentException |
Checknotnull (T) |
Checks The value is not NULL. Returns the value directly, so you can use checknotnull (value) inline. |
NullPointerException |
CheckState (Boolean) |
Checks some state of the object, not dependent on the method arguments. For example, a Iteratormight use this to check that next have been called before any call to remove< /c6>. |
IllegalStateException |
Checkelementindex (int index, int size) |
Checks that index is a valid element index to a list, string, or array with the specified size. An element index could range from 0 inclusive to size exclusive. You don ' t pass the list, string, or array directly; You just pass its size. Returns index. |
Indexoutofboundsexception |
Checkpositionindex (int index, int size) |
Checks that index is a valid position index to a list, string, or array with the specified size. A position index may range from 0 inclusive to size inclusive. You don ' t pass the list, string, or array directly; You just pass its size. Returns index. |
Indexoutofboundsexception |
checkpositionindexes (int start, int end, int size) |
Google Guava Learning Record "II" precondition