MonthApril 2010

Java: Types of Exception

Exception come is several flavours: RuntimeExceptions, Errors, Checked and Unchecked.

Type codes used in describing Exceptions
Letter Type Parent Class Checked?
(declare throws?)
R Runtime java.lang.RuntimeException N Error that can occur in almost any code e.g. NullPointerException.
E Error java.lang.Error N Serious error you really should not try to catch, e.g. OutOfMemoryError.
C Checked java.lang.Exception Y Likely exceptional condition that can only occur in specific places in the code e.g. EOFException.

Collectively, RuntimeException, Error and Exception are derived from Throwable. RuntimeException is derived from Exception, which is derived from Throwable. Error is derived directly from Throwable. If you catch RuntimeException you will catch all manner of run time Exceptions (type R).
If you catch Error you will catch all manner of errors (type E).
If you catch Exception you will catch all manner of checked Exceptions and run time Exceptions (type R+C).
If you catch Throwable, you will catch everything, (Type R+E+C );
If the Exception is checked, you must either fob it off on the caller, with the throws clause or catch it yourself. Unchecked Exceptions are ones like running out of RAM that, in general you can’t do much about, or that are not associated with specific problematic code, or that are very common such as IllegalArgumentException or NullPointerException. You don’t have to catch unchecked Exceptions or explicitly fob them off on the caller with throws. The classification of an Exception is not an exact science. It is a little bit like the arbitrary assignment of gender in French or German to objects. You just have to look it up. There is a major clue, Error Exceptions end in the string “Error” while checked Exceptions and RuntimeExceptions end in the string “Exception”.

Specific Exceptions
Exception Name Type Package Notes
AbstractMethodError E java.lang  
AccessControlException R This is an exception that is thrown whenever a reference is made to a non-existent ACL (Access Control
List). notes.
AccessException C java.rmi Thrown by certain methods of the java.rmi.Naming class.
AclNotFoundException C Thrown whenever a reference is made to a non-existent ACL (Access Control List).
ActivateFailedException C java.rmi.activation thrown by the RMI runtime when activation fails during a remote call to an activatable object.
ActivationException C java.rmi.activation  
AlreadyBoundException C javax.naming  
ApplicationException C org.omg.CORBA.portable Used for reporting application level exceptions between ORBs and stubs
ArithmeticException R java.lang Most commonly a divide by zero. notes.
ArrayIndexOutOfBoundsException R java.lang Can be handled more generically with IndexOutOfBoundsException. notes.
ArrayStoreException R java.lang Thrown to indicate that an attempt has been made to store the wrong type of object into an array of
objects. notes.
AttributeInUseException C  
AttributeModificationException C  
AuthenticationException C javax.naming  
AuthenticationNotSupportedException C javax.naming  
AWTError E java.awt  
AWTError E java/awt  
AWTException C java.awt  
BadLocationException C javax.swing.text This exception is to report bad locations within a document model.
BatchUpdateException C java.sql  
BindException C Signals that an error occurred while attempting to bind a socket to a local address and port
CannotProceedException C javax.naming  
CannotRedoException R javax.swing.undo  
CannotUndoException R javax.swing.undo  
CertificateEncodingException C  
CertificateException C  
CertificateExpiredException C  
CertificateNotYetValidException C  
CertificateParsingException C  
ChangedCharSetException C javax.swing.text  
CharConversionException C  
ClassCastException R java.lang notes.
ClassCircularityError E java.lang  
ClassFormatError E java.lang notes.
ClassNotFoundException C java.lang notes.
CloneNotSupportedException C java.lang  
CMMException R java.awt.color  
CommunicationException C javax.naming  
ConcurrentModificationException R java.util This exception may be thrown by methods that have detected concurrent modification of a backing object
when such modification is not permissible, e. g. two threads modifying a HashMap
simultaneously. notes.
ConfigurationException C javax.naming  
ConnectException C java.rmi  
ConnectIOException C java.rmi  
ContextNotEmptyException C javax.naming  
CRLException C CRL (Certificate Revocation List) Exception.
DataFormatException C  
DigestException C  
EmptyStackException R java.util Thrown by methods in the Stack class to indicate that the stack is empty. Does not refer to the system
EOFException C notes.
Error E java.lang Catches any serious error such as OutOfMemoryError that you unlikely can
recover from.
Exception C java.lang generic. Catches any specify Exception plus general Runtime exceptions, but not Errors.
ExceptionInInitializerError E java.lang notes.
ExceptionInInitializerError E java.lang  
ExpandVetoException C javax.swing.tree  
ExportException C java.rmi.server  
FileNotFoundException C  
FontFormatException C java.awt  
GeneralSecurityException C  
IllegalAccessError E java.lang notes.
IllegalAccessException C java.lang Thrown when an application tries to load in a class, but the currently executing method does not have
access to the definition of the specified class, because the class is not public and in another
IllegalArgumentException R java.lang Most common exception to reject a bad parameter to a method.
IllegalComponentStateException R java.awt  
IllegalMonitorStateException R java.lang  
IllegalPathStateException R java.awt.geom  
IllegalStateException R java.lang Signals that a method has been invoked at an illegal or inappropriate time.
IllegalThreadStateException R java.lang  
ImagingOpException R java.awt.image  
IncompatibleClassChangeError E java.lang notes.
IndexOutOfBoundsException R java.lang Similar to ArrayIndexOutOfBoundsException for ArrayList.
IndirectionException R org.omg.CORBA.portable  
InstantiationError E java.lang  
InstantiationException C java.lang  
InsufficientResourcesException C javax.naming  
InternalError E java.lang  
InterruptedException C java.lang Thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread
interrupts it using the interrupt method in class Thread.
InterruptedIOException C  
InterruptedNamingException C javax.naming  
IntrospectionException C java.beans  
InvalidAlgorithmParameterException C This is a GeneralSecurityException. See IllegalArgumentException.
InvalidAttributeIdentifierException C  
InvalidAttributesException C  
InvalidAttributeValueException C  
InvalidClassException C notes.
InvalidDnDOperationException R java.awt.dnd  
InvalidKeyException C  
InvalidKeySpecException C  
InvalidMidiDataException C javax.sound.midi  
InvalidNameException C javax.naming  
InvalidObjectException C  
InvalidParameterException R  
InvalidParameterSpecException C  
InvalidSearchControlsException C  
InvalidSearchFilterException C  
InvalidTransactionException C javax.transaction  
InvocationTargetException C java.lang.reflect  
IOException C  
JarException C java.util.jar  
KeyException C  
KeyManagementException C  
KeyStoreException C  
LastOwnerException C  
LdapReferralException C javax.naming.ldap  
LimitExceededException C javax.naming  
LineUnavailableException C javax.sound.sampled  
LinkageError E java.lang  
LinkException C javax.naming  
LinkLoopException C javax.naming  
MalformedLinkException C javax.naming  
MalformedURLException C  
MarshalException C java.rmi  
MidiUnavailableException C javax.sound.midi  
MimeTypeParseException C java.awt.datatransfer  
MissingResourceException R java.util  
NameAlreadyBoundException C javax.naming  
NameNotFoundException C javax.naming  
NamingException C javax.naming  
NamingSecurityException C javax.naming  
NegativeArraySizeException R java.lang  
NoClassDefFoundError E java.lang notes.
NoInitialContextException C javax.naming  
NoninvertibleTransformException C java.awt.geom  
NoPermissionException C javax.naming  
NoRouteToHostException C  
NoSuchAlgorithmException C  
NoSuchAttributeException C  
NoSuchElementException R java.util  
NoSuchFieldError E java.lang  
NoSuchFieldException C java.lang  
NoSuchMethodError E java.lang notes.
NoSuchMethodException C java.lang  
NoSuchObjectException C java.rmi  
NoSuchProviderException C notes.
NotActiveException C Thrown when serialization or deserialization is not active
NotBoundException C java.rmi  
NotContextException C javax.naming  
NotOwnerException C  
NotSerializableException C notes.
NullPointerException R java.lang Actually a null reference exception. notes.
NumberFormatException R java.lang Commonly thrown when a String is converted to internal binary numeric format. notes.
ObjectStreamException C  
OperationNotSupportedException C javax.naming  
OptionalDataException C Unexpected data appeared in an ObjectInputStream trying to read an Object. Occurs when the stream
contains primitive data instead of the object that is expected by readObject. The EOF flag in the exception
is true indicating that no more primitive data is available. The count field contains the number of bytes
available to read.
OutOfMemoryError E java.lang By the time this happens it is almost too late. gc has already done what it could. Possibly some
process has just started gobbling RAM, or perhaps the problem you are trying to solve is just too big for
the size of the allotted virtual ram. You can control that with the java.exe command line switches.
ParseException C java.text  
PartialResultException C javax.naming  
PolicyError E org.omg.CORBA  
PrinterAbortException C java.awt.print  
PrinterException C java.awt.print  
PrinterIOException C java.awt.print  
PrivilegedActionException C  
ProfileDataException R java.awt.color  
PropertyVetoException C java.beans  
ProtocolException C  
ProviderException R  
RasterFormatException R java.awt.image  
ReferralException C javax.naming  
RemarshalException C org.omg.CORBA.portable  
RemoteException C java.rmi  
RMISecurityException C java.rmi  
RuntimeException R java.lang Error that can occur in almost any code e.g. NullPointerException. Use this
when to catch general errors when no specific exception is being thrown.
SchemaViolationException C  
SecurityException R java.lang  
ServerCloneException C java.rmi.server  
ServerError E java.rmi  
ServerException C java.rmi  
ServerNotActiveException C java.rmi.server  
ServerRuntimeException C java.rmi  
ServiceUnavailableException C javax.naming  
SignatureException C  
SizeLimitExceededException C javax.naming  
SkeletonMismatchException C java.rmi.server  
SkeletonNotFoundException C java.rmi.server  
SocketException C  
SocketSecurityException C java.rmi.server  
SQLException C java.sql  
StackOverflowError E java.lang notes.
StreamCorruptedException C ObjectStream data are scrambled. notes.
StringIndexOutOfBoundsException R java.lang Can be handled more generically with IndexOutOfBoundsException. notes.
StubNotFoundException C java.rmi  
SyncFailedException C  
SystemException R org.omg.CORBA  
TimeLimitExceededException C javax.naming  
TooManyListenersException C java.util  
TransactionRequiredException C javax.transaction  
TransactionRolledbackException C javax.transaction  
UndeclaredThrowableException R java.lang.reflect  
UnexpectedException R java.rmi  
UnknownError E java.lang  
UnknownException R org.omg.CORBA.portable  
UnknownGroupException C java.rmi.activation  
UnknownHostException C java.rmi  
UnknownHostException C  
UnknownObjectException C java.rmi.activation  
UnknownServiceException C  
UnknownUserException C org.omg.CORBA  
UnmarshalException C java.rmi notes.
UnrecoverableKeyException C  
UnsatisfiedLinkError E java.lang notes.
UnsupportedAudioFileException C javax.sound.sampled  
UnsupportedClassVersionError E java.lang notes.
UnsupportedDataTypeException C undocumented. notes.
UnsupportedEncodingException C  
UnsupportedFlavorException C java.awt.datatransfer  
UnsupportedLookAndFeelException C javax.swing  
UnsupportedOperationException R java.lang Use for code not yet implemented, or that you deliberately did not implement.
UserException C org.omg.CORBA  
UTFDataFormatException C  
VerifyError E java.lang notes.
VirtualMachineError E java.lang  
WriteAbortedException C  
ZipException C notes.

Java Exception: StackOverflowError

StackOverflowError Stack size too small. Use java -Xss to increase default stacksize.

These usually happen when you have recursion, a method that calls itself, perhaps indirectly via a second method. You have simply nested to deeply. Another source of the problem is calling method x() or this.x() when you meant to call super. x(), usually when inside method x. If you legitimately overflowed the stack, you may rescue yourself by getting the runtime to allocate more memory for the stack for each thread with java.exe -Xss128

© 2022 Ziben IT Solutions

Theme by Anders NorénUp ↑