Sunday, February 6, 2011

Exception Handling in JAVA

Exceptions let you handle unusual error conditions without cluttering your code with nested ifs after every method call. There are two kinds of Exceptions, checked and unchecked. You don’t need to declare unchecked Exceptions, those that derive from java.lang.Error and java.lang.RuntimeException, in your throws clauses. Checked Exceptions usually derive from java.lang.Exception.
The basic syntax to handle an Exception looks like this:
The
String trickyMethod() throws IOException
{
   int result = readAnotherChar();
   if ( result < 0 ) throw new IOException( "bad data" );
   return result;
}

Collectively, RuntimeExceptionError and Exception are derived from ThrowableRuntimeException is derived from Exception, which is derived fromThrowableError 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 orNullPointerException. 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”.
Exception Name
Type
Package

AbstractMethodError
E
java.lang

AccessControlException
R
java.security

AccessException
C
java.rmi

AclNotFoundException
C
java.security.acl

ActivateFailedException
C
java.rmi.activation

ActivationException
C
java.rmi.activation

AlreadyBoundException
C
javax.naming

ApplicationException
C
org.omg.CORBA.portable

ArithmeticException
R
java.lang

ArrayIndexOutOfBoundsException
R
java.lang

ArrayStoreException
R
java.lang

AttributeInUseException
C
javax.naming.directory

AttributeModificationException
C
javax.naming.directory

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

BatchUpdateException
C
java.sql

BindException
C
java.net

CannotProceedException
C
javax.naming

CannotRedoException
R
javax.swing.undo

CannotUndoException
R
javax.swing.undo

CertificateEncodingException
C
java.security.cert

CertificateException
C
java.security.cert

CertificateExpiredException
C
java.security.cert

CertificateNotYetValidException
C
java.security.cert

CertificateParsingException
C
java.security.cert

ChangedCharSetException
C
javax.swing.text

CharConversionException
C
java.io

ClassCastException
R
java.lang

ClassCircularityError
E
java.lang

ClassFormatError
E
java.lang

ClassNotFoundException
C
java.lang

CloneNotSupportedException
C
java.lang

CMMException
R
java.awt.color

CommunicationException
C
javax.naming

ConcurrentModificationException
R
java.util

ConfigurationException
C
javax.naming

ConnectException
C
java.rmi

ConnectIOException
C
java.rmi

ContextNotEmptyException
C
javax.naming

CRLException
C
java.security.cert

DataFormatException
C
java.util.zip

DigestException
C
java.security

EmptyStackException
R
java.util

EOFException
C
java.io

Error
E
java.lang

Exception
C
java.lang

ExceptionInInitializerError
E
java.lang

ExceptionInInitializerError
E
java.lang

ExpandVetoException
C
javax.swing.tree

ExportException
C
java.rmi.server

FileNotFoundException
C
java.io

FontFormatException
C
java.awt

GeneralSecurityException
C
java.security

IllegalAccessError
E
java.lang

IllegalAccessException
C
java.lang

IllegalArgumentException
R
java.lang

IllegalComponentStateException
R
java.awt

IllegalMonitorStateException
R
java.lang

IllegalPathStateException
R
java.awt.geom

IllegalStateException
R
java.lang

IllegalThreadStateException
R
java.lang

ImagingOpException
R
java.awt.image

IncompatibleClassChangeError
E
java.lang

IndexOutOfBoundsException
R
java.lang

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

InterruptedIOException
C
java.io

InterruptedNamingException
C
javax.naming

IntrospectionException
C
java.beans

InvalidAlgorithmParameterException
C
java.security

InvalidAttributeIdentifierException
C
javax.naming.directory

InvalidAttributesException
C
javax.naming.directory

InvalidAttributeValueException
C
javax.naming.directory

InvalidClassException
C
java.io

InvalidDnDOperationException
R
java.awt.dnd

InvalidKeyException
C
java.security

InvalidKeySpecException
C
java.security.spec

InvalidMidiDataException
C
javax.sound.midi

InvalidNameException
C
javax.naming

InvalidObjectException
C
java.io

InvalidParameterException
R
java.security

InvalidParameterSpecException
C
java.security.spec

InvalidSearchControlsException
C
javax.naming.directory

InvalidSearchFilterException
C
javax.naming.directory

InvalidTransactionException
C
javax.transaction

InvocationTargetException
C
java.lang.reflect

IOException
C
java.io

JarException
C
java.util.jar

KeyException
C
java.security

KeyManagementException
C
java.security

KeyStoreException
C
java.security

LastOwnerException
C
java.security.acl

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
java.net

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

NoInitialContextException
C
javax.naming

NoninvertibleTransformException
C
java.awt.geom

NoPermissionException
C
javax.naming

NoRouteToHostException
C
java.net

NoSuchAlgorithmException
C
java.security

NoSuchAttributeException
C
javax.naming.directory

NoSuchElementException
R
java.util

NoSuchFieldError
E
java.lang

NoSuchFieldException
C
java.lang

NoSuchMethodError
E
java.lang

NoSuchMethodException
C
java.lang

NoSuchObjectException
C
java.rmi

NoSuchProviderException
C
java.security

NotActiveException
C
java.io

NotBoundException
C
java.rmi

NotContextException
C
javax.naming

NotOwnerException
C
java.security.acl

NotSerializableException
C
java.io

NullPointerException
R
java.lang

NumberFormatException
R
java.lang

ObjectStreamException
C
java.io

OperationNotSupportedException
C
javax.naming

OptionalDataException
C
java.io

OutOfMemoryError
E
java.lang

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
java.security

ProfileDataException
R
java.awt.color

PropertyVetoException
C
java.beans

ProtocolException
C
java.net

ProviderException
R
java.security

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

SchemaViolationException
C
javax.naming.directory

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
java.security

SizeLimitExceededException
C
javax.naming

SkeletonMismatchException
C
java.rmi.server

SkeletonNotFoundException
C
java.rmi.server

SocketException
C
java.net

SocketSecurityException
C
java.rmi.server

SQLException
C
java.sql

StackOverflowError
E
java.lang

StreamCorruptedException
C
java.io

StringIndexOutOfBoundsException
R
java.lang

StubNotFoundException
C
java.rmi

SyncFailedException
C
java.io

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
java.net

UnknownObjectException
C
java.rmi.activation

UnknownServiceException
C
java.net

UnknownUserException
C
org.omg.CORBA

UnmarshalException
C
java.rmi

UnrecoverableKeyException
C
java.security

UnsatisfiedLinkError
E
java.lang

UnsupportedAudioFileException
C
javax.sound.sampled

UnsupportedClassVersionError
E
java.lang

UnsupportedDataTypeException
C
java.io

UnsupportedEncodingException
C
java.io

UnsupportedFlavorException
C
java.awt.datatransfer

UnsupportedLookAndFeelException
C
javax.swing

UnsupportedOperationException
R
java.lang

UserException
C
org.omg.CORBA

UTFDataFormatException
C
java.io

VerifyError
E
java.lang

VirtualMachineError
E
java.lang

WriteAbortedException
C
java.io

ZipException
C
java.util.zip



No comments:

Post a Comment