Skip to content

Instantly share code, notes, and snippets.

@7yl4r
Last active July 27, 2021 17:26
Show Gist options
  • Save 7yl4r/9e3f223f2eca76104940 to your computer and use it in GitHub Desktop.
Save 7yl4r/9e3f223f2eca76104940 to your computer and use it in GitHub Desktop.
info on Java built-in exceptions from perspective of developer looking to reuse built-in exceptions

organized by estimated utility

IllegalArgumentException

Thrown to indicate that a method has been passed an illegal or inappropriate argument.

IndexOutOfBoundsException

Thrown to indicate that an index of some sort (such as to an array, to a string, or to a vector) is out of range.

ArithmeticException

Thrown when the requested mathematical operation is non-sensical or impossible. example: int x = 1/0;

KeySelectorException

I think it makes sense to throw this if you are trying to looking for an object using a key and it was not found or the key is otherwise invalid, but I don't really understand the dev docs on it.

example: myDataStructure.get("lookup_key"); when lookup_key is not in the data structure.

IllegalStateException

The application is not in an appropriate state for the requested operation. example: trying to save before file is loaded or created.

UnsupportedOperationException

Thrown to indicate that the requested operation is not supported. Useful for not-yet-implemented operations or if some operation is specified by a method parameter.

TimeoutException

Throw this if something took too long and you're giving up.

DataFormatException

Throw this when you have recieved improperly formatted data. example: MyClass.applyJSONString("{non:sense,all,garbled=definitely.not;json{{{")

IOException

Having some problem reading/writing? Throw this exception.

ScriptException

Running a script of some form and found a problem with it (not I/O or parsing)? Throw this exception.

GeneralSecurityException

Throw this if you encounter a security-related issue.

RuntimeException

Use this for some runtime-error that doesn't fit well into any other category.

A list of exceptions I have reviewed in creating the above list of reusable exceptions.

java dev doc Exception sub-classes:

AclNotFoundException

This is an exception that is thrown whenever a reference is made to a non-existent ACL (Access Control List).

ActivationException

General exception used by the activation interfaces.

As of release 1.4, this exception has been retrofitted to conform to the general purpose exception-chaining mechanism. The "detail exception" that may be provided at construction time and accessed via the public detail field is now known as the cause, and may be accessed via the Throwable.getCause() method, as well as the aforementioned "legacy field."

Invoking the method Throwable.initCause(Throwable) on an instance of ActivationException always throws IllegalStateException.

AlreadyBoundException

An AlreadyBoundException is thrown if an attempt is made to bind an object in the registry to a name that already has an associated binding.

ApplicationException

This class is used for reporting application level exceptions between ORBs and stubs.

AWTException,

BackingStoreException

Thrown to indicate that a preferences operation could not complete because of a failure in the backing store, or a failure to contact the backing store.

BadAttributeValueExpException, Thrown when an invalid MBean attribute is passed to a query constructing method. This exception is used internally by JMX during the evaluation of a query. User code does not usually see it.

BadBinaryOpValueExpException

BadLocationException

This exception is to report bad locations within a document model (that is, attempts to reference a location that doesn't exist).

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see XMLEncoder.

BadStringOperationException

Thrown when an invalid string operation is passed to a method for constructing a query.

BrokenBarrierException,

CertificateException,

ClassNotFoundException,

CloneNotSupportedException,

DataFormatException

Signals that a data format error has occurred.

DatatypeConfigurationException,

DestroyFailedException,

ExecutionException

Exception thrown when attempting to retrieve the result of a task that aborted by throwing an exception. This exception can be inspected using the Throwable.getCause() method.

ExpandVetoException,

FontFormatException,

GeneralSecurityException

The GeneralSecurityException class is a generic security exception class that provides type safety for all the security-related exception classes that extend from it.

GSSException

This exception is thrown whenever a GSS-API error occurs, including any mechanism specific error. It may contain both the major and the minor GSS-API status codes. Major error codes are those defined at the GSS-API level in this class. Minor error codes are mechanism specific error codes that can provide additional information. The underlying mechanism implementation is responsible for setting appropriate minor status codes when throwing this exception. Aside from delivering the numeric error codes to the caller, this class performs the mapping from their numeric values to textual representations.

IllegalAccessException

An IllegalAccessException is thrown when an application tries to reflectively create an instance (other than an array), set or get a field, or invoke a method, but the currently executing method does not have access to the definition of the specified class, field, method or constructor.

IllegalClassFormatException,

InstantiationException,

InterruptedException,

IntrospectionException,

Thrown when an exception happens during Introspection.

Typical causes include not being able to map a string class name to a Class object, not being able to resolve a string method name, or specifying a method name that has the wrong type signature for its intended use.

InvalidApplicationException,

InvalidMidiDataException,

InvalidPreferencesFormatException,

InvalidTargetObjectTypeException,

InvocationTargetException,

IOException

Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations.

JAXBException,

JMException,

KeySelectorException

Indicates an exceptional condition thrown by a KeySelector.

A KeySelectorException can contain a cause: another throwable that caused this KeySelectorException to get thrown.

LastOwnerException,

LineUnavailableException,

MarshalException

Indicates an exceptional condition that occured during the XML marshalling or unmarshalling process.

MidiUnavailableException,

MimeTypeParseException,

MimeTypeParseException,

NamingException

This is the superclass of all exceptions thrown by operations in the Context and DirContext interfaces. The nature of the failure is described by the name of the subclass. This exception captures the information pinpointing where the operation failed, such as where resolution last proceeded to.

Resolved Name. Portion of name that has been resolved. Resolved Object. Object to which resolution of name proceeded. Remaining Name. Portion of name that has not been resolved. Explanation. Detail explaining why name resolution failed. Root Exception. The exception that caused this naming exception to be thrown. null is an acceptable value for any of these fields. When null, it means that no such information has been recorded for that field. A NamingException instance is not synchronized against concurrent multithreaded access. Multiple threads trying to access and modify a single NamingException instance should lock the object.

This exception has been retrofitted to conform to the general purpose exception-chaining mechanism. The root exception (or root cause) is the same object as the cause returned by the Throwable.getCause() method.

NoninvertibleTransformException,

NoSuchFieldException

Signals that the class doesn't have a field of a specified name.

NoSuchMethodException

Thrown when a particular method cannot be found.

NotBoundException

A NotBoundException is thrown if an attempt is made to lookup or unbind in the registry a name that has no associated binding.

NotOwnerException,

ParseException

Signals that an error has been reached unexpectedly while parsing.

ParserConfigurationException,

PrinterException,

PrintException,

PrivilegedActionException,

PropertyVetoException,

RefreshFailedException,

RemarshalException,

RuntimeException

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.

A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.

SAXException,

ScriptException

The generic Exception class for the Scripting APIs. Checked exception types thrown by underlying scripting implementations must be wrapped in instances of ScriptException. The class has members to store line and column numbers and filenames if this information is available.

ServerNotActiveException,

SOAPException,

SQLException,

TimeoutException

Exception thrown when a blocking operation times out. Blocking operations for which a timeout is specified need a means to indicate that the timeout has occurred. For many such operations it is possible to return a value that indicates timeout; when that is not possible or desirable then TimeoutException should be declared and thrown.

TooManyListenersException,

TransformerException, TransformException, UnmodifiableClassException, UnsupportedAudioFileException, UnsupportedCallbackException, UnsupportedFlavorException, UnsupportedLookAndFeelException, URIReferenceException, URISyntaxException,

UserException

The root class for CORBA IDL-defined user exceptions. All CORBA user exceptions are checked exceptions, which means that they need to be declared in method signatures.

XAException, XMLParseException, XMLSignatureException, XMLStreamException, XPathException

RuntimeException subclasses

AnnotationTypeMismatchException,

ArithmeticException

Thrown when an exceptional arithmetic condition has occurred. For example, an integer "divide by zero" throws an instance of this class.

ArrayStoreException

Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects. For example, the following code generates an ArrayStoreException:

 Object x[] = new String[3];
 x[0] = new Integer(0);

BufferOverflowException, BufferUnderflowException, CannotRedoException, CannotUndoException, ClassCastException, CMMException, ConcurrentModificationException, DataBindingException, DOMException, EmptyStackException, EnumConstantNotPresentException,

EventException

Event operations may throw an EventException as specified in their method descriptions.

See also the Document Object Model (DOM) Level 2 Events Specification.

IllegalArgumentException

Thrown to indicate that a method has been passed an illegal or inappropriate argument.

IllegalMonitorStateException,

IllegalPathStateException,

IllegalStateException

Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation.

##ImagingOpException,

IncompleteAnnotationException,

IndexOutOfBoundsException

Thrown to indicate that an index of some sort (such as to an array, to a string, or to a vector) is out of range.

Applications can subclass this class to indicate similar exceptions.

JMRuntimeException,

LSException, MalformedParameterizedTypeException, MirroredTypeException, MirroredTypesException, MissingResourceException, NegativeArraySizeException,

NoSuchElementException

Thrown by the nextElement method of an Enumeration to indicate that there are no more elements in the enumeration.

NoSuchMechanismException,

NullPointerException,

ProfileDataException,

ProviderException,

RasterFormatException,

RejectedExecutionException,

SecurityException

Thrown by the security manager to indicate a security violation.

SystemException,

TypeConstraintException,

TypeNotPresentException,

UndeclaredThrowableException, UnknownAnnotationValueException, UnknownElementException, UnknownTypeException, UnmodifiableSetException,

UnsupportedOperationException

Thrown to indicate that the requested operation is not supported.

This class is a member of the Java Collections Framework.

WebServiceException

@grv87
Copy link

grv87 commented Aug 8, 2018

KeySelectorException is thrown by KeySelector. It makes sense in the context of XML validating only.
For your example myDataStructure.get("lookup_key") StringIndexOutOfBoundsException fits better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment