zoukankan      html  css  js  c++  java
  • Google Guava 学习记录《二》 Precondition

    1       int i = 4;  
    2          int j = 5;
    3          Preconditions.checkArgument(i>j,"i should bigger than j, but i is %s and j is %s",i,j);
    Exception in thread "main" java.lang.IllegalArgumentException: i should bigger than j, but i is 4 and j is 5
        at com.google.common.base.Preconditions.checkArgument(Preconditions.java:145)
        at google.guava.GuavaDemo.main(GuavaDemo.java:39)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

    先放上最常用的checkArgument,可以看到当没有满足表达式时,输出的错误消息可以自己定义,而且非常清晰。

    以下是对3个参数的解释:

    • No extra arguments. Any exceptions are thrown without error messages. 不要错误消息
    • An extra Object argument. Any exception is thrown with the error message object.toString(). 只要错误消息
    • An extra String argument, with an arbitrary number of additional Object arguments. This behaves something like printf, but for GWT compatibility and efficiency, it only allows %s indicators  加上变量,然后出错时查看是不是数值确实有错误

      其他的应该用的很少,在项目中ack发现只有用到过checkArgument了。。就不做解释了

    checkArgument(boolean) Checks that the boolean is true. Use for validating arguments to methods. IllegalArgumentException
    checkNotNull(T) Checks that 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, an Iteratormight use this to check that next has been called before any call to remove. IllegalStateException
    checkElementIndex(int index, int size) Checks that index is a valid element index into a list, string, or array with the specified size. An element index may 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 into 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)
  • 相关阅读:
    C语言学习之我见-strcpy()字符串复制函数
    豆邮windows客户端(第三方)开发详解
    项目管理的主要控制因素(转)
    daterangepicker -- bootstrap日期时间范围插件使用分享
    Jacob
    Selenium
    基本套接字总结(@function)
    Python字符串处理NoneType的处理
    机器学习相关会议(转载)
    jboss-AS目录结构了解(资料摘取)
  • 原文地址:https://www.cnblogs.com/-Doraemon/p/4721475.html
Copyright © 2011-2022 走看看