Java examples for Language Basics:throw
Use the throw keyword to throw a specified exception.
public class Main { public static void main(String[] args) { try {/* ww w. j a va2 s .c om*/ 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"); } }