2. Throwing exceptions

throw someThrowableObject;

Example:

public Object pop() throws EmptyStackException {
   Object obj;
   if (size == 0) throw new EmptyStackException();
   obj = objectAt(size - 1);
   setObjectAt(size - 1, null);
   size--;
   return obj;
}

Figure 6.4. Throwable and its Subclasses

Throwable and its Subclasses

2.2.1. Errors

When a dynamic linking failure or some other "hard" failure in the virtual machine occurs, the virtual machine throws an Error.

2.2.2. Exceptions

Most programs throw and catch objects that derive from the Exception class.

2.2.3. Runtime Exceptions

The RuntimeException class represents exceptions that occur within the Java virtual machine (during runtime).

Audio in Portuguese