The java.lang.RuntimeException Class
Runtime exceptions are all subclasses of the java.lang.RuntimeException class, which is a subclass of the Exception class. As these runtime exceptions are usually caused by program bugs that should not occur in the first place, it is usually more appropriate to treat them as faults in the program design and let them be handled by the default exception handler.
Click here to view code image
java.lang.ArithmeticException
This exception represents situations where an illegal arithmetic operation is attempted, such as integer division by 0. It is typically thrown by the JVM.
Click here to view code image
java.lang.ArrayIndexOutOfBoundsException
Java provides runtime checking of the array index value, meaning out-of-bounds array indices. The subclass ArrayIndexOutOfBoundsException of the RuntimeException class represents exceptions thrown by the JVM that signal out-of-bound errors specifically for arrays—that is, an error in which an invalid index is used to access an element in the array. The index value must satisfy the relation 0 ≤ index value < length of the array (§3.9, p. 120).
Click here to view code image
java.lang.ArrayStoreException
This exception is thrown by the JVM when an attempt is made to store an object of the wrong type into an array of objects. The array store check at runtime ensures that an object being stored in the array is assignment compatible with the element type of the array (§5.7, p. 260). To make the array store check feasible at runtime, the array retains information about its declared element type at runtime.
java.lang.ClassCastException
This exception is thrown by the JVM to signal that an attempt was made to cast a reference value to a type that was not legal, such as casting the reference value of an Integer object to the Long type (§5.11, p. 269).
Click here to view code image
java.lang.IllegalArgumentException
The class IllegalArgumentException represents exceptions thrown to signal that a method was called with an illegal or inappropriate argument. For example, the ofPattern(String pattern) method in the java.time.format.DateTimeFormatter class throws an IllegalArgumentException when the letter pattern passed as an argument is invalid (§18.6, p. 1134).
Click here to view code image
java.lang.IllegalThreadStateException
The class IllegalThreadStateException is a subclass of the IllegalArgumentException class. Certain operations on a thread can only be executed when the thread is in an appropriate state (§22.4, p. 1380). This exception is thrown when this is not the case. For example, the start() method of a Thread object throws this exception if the thread has already been started (§22.3, p. 1370).
Click here to view code image
java.lang.NumberFormatException
The class NumberFormatException is a subclass of the IllegalArgumentException class that is specialized to signal problems when converting a string to a numeric value if the format of the characters in the string is not appropriate for the conversion. This exception is thrown programmatically. The numeric wrapper classes all have methods that throw this exception when conversion from a string to a numeric value is not possible (§8.3, p. 434).
Click here to view code image
java.lang.NullPointerException
This exception is typically thrown by the JVM when an attempt is made to use the null value as a reference value to refer to an object. This might involve calling an instance method using a reference that has the null value, or accessing a field using a reference that has the null value.
This programming error has made this exception one of the most frequently thrown exceptions by the JVM. The error message issued provides helpful information as to where and which reference raised the exception. However, inclusion of variables names in the message can be a potential security risk.
Click here to view code image
Exception in thread “main” java.lang.NullPointerException: Cannot invoke
“String.toLowerCase()” because “msg” is null
at StringMethods.main(StringMethods.java:162)
Click here to view code image
java.lang.UnsupportedOperationException
This exception is thrown programmatically to indicate that an operation invoked on an object is not supported. Typically, a class implements an interface, but chooses not to provide certain operations specified in the interface. Methods in the class corresponding to these operations throw this exception to indicate that an operation is not supported by the objects of the class.
The API documentation of the java.util.Collection interface (§15.1, p. 783) in the Java Collections Framework states that certain methods are optional, meaning that a concrete collection class need not provide support for such operations, and if any such operation is not supported, then the method should throw this exception.
java.time.DateTimeException
In the Date and Time API, the class DateTimeException represents exceptions that signal problems with creating, querying, and manipulating date-time objects (§17.2, p. 1027).
Click here to view code image
java.time.format.DateTimeParseException
In the Date and Time API, the class DateTimeParseException is a subclass of the Date-TimeException class that represents an exception that signals unexpected errors when parsing date and time values (§18.6, p. 1127).
Click here to view code image
java.util.MissingResourceException
This exception is thrown programmatically, typically by the lookup methods of the java.util.ResourceBundle class (§18.3, p. 1104). Get methods on resource bundles typically throw this exception when no resource can be found for a given key. Static lookup methods in this class also throw this exception when no resource bundle can be found based on a specified base name for a resource bundle. Resource bundles are discussed in §18.3, p. 1102.
The java.lang.Error Class
The class Error and its subclasses define errors that are invariably never explicitly caught and are usually irrecoverable. Not surprisingly, most such errors are signaled by the JVM. Apart from the subclass mentioned below, other subclasses of the java.lang.Error class define different categories of errors.
The subclass VirtualMachineError represents virtual machine errors like stack overflow (StackOverflowError) and out of memory for object allocation (OutOfMemoryError). The subclass LinkageError represents class linkage errors like missing class definitions (NoClassDefFoundError). The subclass AssertionError of the Error class is used by the Java assertion facility.