- Example: NullPointerException
String name = null;
int n = name.length(); // ERROR
- Cannot apply a method to null
- Virtual machine throws
exception
- Unless there is a handler,
program exits with stack trace
Exception in thread "main" java.lang.NullPointerException
at Greeter.sayHello(Greeter.java:25)
at GreeterTest.main(GreeterTest.java:6)
- Indicate error conditions by throwing exception object.
For instance, for parameter validation:
public double sqrt(double x)
{
if (x < 0.0)
throw new IllegalArgumentException("invalid parameter: negative value");
....
}
-
javadoc for
java.lang.Exception