Like any object in Java, you always create exceptions on the heap using new, which allocates storage and calls a constructor. There are two constructors in all standard exceptions; The first is the default constructor, and the second takes a string argument so you can place pertinent information in the exception:
throw new NullPointerException("t = null");
This string can later be extracted using various methods, as youll see.
The keyword throw causes a number of relatively magical things to happen. Typically, youll first use new to create an object that represents the error condition. You give the resulting reference to throw. The object is, in effect, returned from the method, even though that object type isnt normally what the method is designed to return. A simplistic way to think about exception handling is as a different kind of return mechanism, although you get into trouble if you take that analogy too far. You can also exit from ordinary scopes by throwing an exception. But a value is returned, and the method or scope exits.
Any similarity to an ordinary return from a method ends here, because where you return is someplace completely different from where you return for a normal method call. (You end up in an appropriate exception handler that might be farmany levels away on the call stackfrom where the exception was thrown.)
In addition, you can throw any type of Throwable (the exception root class) object that you want. Typically, youll throw a different class of exception for each different type of error. The information about the error is represented both inside the exception object and implicitly in the name of the exception class, so someone in the bigger context can figure out what to do with your exception. (Often, the only information is the type of exception, and nothing meaningful is stored within the exception object.)