Share via


11.5.1 The Classes Exception and RuntimeException

The class Exception is the superclass of all the exceptions that ordinary programs may wish to recover from.

11.5.1.1 Standard Runtime Exceptions

The class RuntimeException is a subclass of class Exception. The subclasses of RuntimeException are unchecked exception classes.

Package java.lang defines the following standard unchecked runtime exceptions, which, like all other classes in package java.lang, are implicitly imported and therefore may be referred to by their simple names:

  • ArithmeticException: An exceptional arithmetic situation has arisen, such as an integer division (§15.16.2) operation with a zero divisor.
  • ArrayStoreException: An attempt has been made to store into an array component a value whose class is not assignment compatible with the component type of the array (§10.10, §15.25.1).
  • ClassCastException: An attempt has been made to cast (§5.4, §15.15) a reference to an object to an inappropriate type.
  • IllegalArgumentException: A method was passed an invalid or inappropriate argument or invoked on an inappropriate object. Subclasses of this class include:
    • IllegalThreadStateException: A thread was not in an appropriate state for a requested operation.
    • NumberFormatException: An attempt was made to convert a String to a value of a numeric type, but the String did not have an appropriate format.
  • IllegalMonitorStateException: A thread has attempted to wait on (§20.1.6, §20.1.7, §20.1.8) or notify (§20.1.9, §20.1.10) other threads waiting on an object that it has not locked.
  • IndexOutOfBoundsException: Either an index of some sort (such as to an array, a string, or a vector) or a subrange, specified either by two index values or by an index and a length, was out of range.
  • NegativeArraySizeException: An attempt was made to create an array with a negative length (§15.9).
  • NullPointerException: An attempt was made to use a null reference in a case where an object reference was required.
  • SecurityException: A security violation was detected (§20.17).

Package java.util defines the following additional standard unchecked runtime exceptions:

  • java.util.EmptyStackException: An attempt was made to access an element of an empty stack.
  • java.util.NoSuchElementException: An attempt was made to access an element of an empty vector.

11.5.1.2 Standard Checked Exceptions

The standard subclasses of Exception other than RuntimeException are all checked exception classes.

Package java.lang defines the following standard exceptions, which, like all other classes in package java.lang, are implicitly imported and therefore may be referred to by their simple names:

  • ClassNotFoundException: A class or interface with a specified name could not be found (§20.3.8).
  • CloneNotSupportedException: The clone method (§20.1.5) of class Object has been invoked to clone an object, but the class of that object does not implement the Cloneable interface.
  • IllegalAccessException: An attempt has been made to load a class using a string giving its fully qualified name, but the currently executing method does not have access to the definition of the specified class because the class is not public and is in another package.
  • InstantiationException: An attempt was made to create an instance of a class using the newInstance method in class Class, but the specified class object cannot be instantiated because it is an interface, is abstract, or is an array.
  • InterruptedException: The current thread was waiting, and another thread has interrupted the current thread, using the interrupt method of class Thread(§20.20.31).

Package java.io defines the following additional standard exceptions:

  • java.io.IOException: A requested I/O operation could not be completed normally. Subclasses of this class include:
    • java.io.EOFException: End of file has been encountered before normal completion of an input operation.
    • java.io.FileNotFoundException: A file with the name specified by a file name string or path was not found within the file system.
    • java.io.InterruptedIOException: The current thread was waiting for completion of an I/O operation, and another thread has interrupted the current thread, using the interrupt method of class Thread(§20.20.31).
    • java.io.UTFDataFormatException: A requested conversion of a string to or from Java modified UTF-8 format could not be completed (§22.1.15, §22.2.14) because the string was too long or because the purported UTF-8 data was not the result of encoding a Unicode string into UTF-8.

The standard package java.net defines the following additional subclasses of java.io.IOException:

u java.net.MalformedURLException: A string that was provided as a URL, or as part of a URL, had an inappropriate format or specified an unknown protocol.

  • java.net.ProtocolException: Some aspect of a network protocol was not correctly carried out.
  • java.net.SocketException: An operation involving a socket could not be completed normally.
  • java.net.UnknownHostException: The name of a network host could not be resolved to a network address.
  • java.net.UnknownServiceException: The network connection cannot support the requested service.