提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用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 提供 }