/** * Title: NullPointer * Description: Java runtime exception handling and why we don't need to * check for null pointers, etc. * @author hollingd@cs.rpi.edu */ import java.util.*; public class NullPointer { public static void main(String[] args) { Date f = new Date(); f=null; //PrintSomething2(f); // comment out to test PrintSomething PrintSomething(f); } // no checks, if x is null runtime exception default behavior static void PrintSomething(Object x) { System.out.println(x.getClass().getName()); } // we explicitly check for runtime exception! static void PrintSomething2(Object x) { try { System.out.println(x.getClass().getName()); } catch (RuntimeException re) { System.out.println("Fatal Error!"); System.exit(1); } } }