集册 Java实例教程 抛出异常

抛出异常

欢马劈雪     最近更新时间:2020-01-02 10:19:05

675
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用throw关键字引发指定的异常。

public class Main {

  public static void main(String[] args) {


    try {
    /**
    来 自 nowjava
    **/

      callSomeFunctionThatMightThrow(null);

    } catch (NullPointerException e) {

      System.out.println("There was an null parameter!");

    }


  }


  private static void callSomeFunctionThatMightThrow(Object o) {

    if (o == null)

      throw new NullPointerException("The object is null");


  }//N  o w  J a v a . c o m 提供

}