springframework中Assert的用法

使用assert的好处就是比较简洁,不用加try catch就可以附加一些预期的提示信息,方便定位问题

1 import org.springframework.util.Assert;
2 
3 public class Demo {
4     public static void main(String[] args) {
5         String location = null;
6         Assert.notNull(location, "Location must not be null");
7         System.out.println("location = [" + location + "]");
8     }
9 }
Demo

Output:

1 Exception in thread "main" java.lang.IllegalArgumentException: Location must not be null
2     at org.springframework.util.Assert.notNull(Assert.java:115)
3     at Demo.main(Demo.java:9)
4     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
5     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
6     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
7     at java.lang.reflect.Method.invoke(Method.java:498)
8     at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
View Code

猜你喜欢

转载自www.cnblogs.com/TomJay/p/9670670.html