The hierarchy of exception classes commence from Throwable class which is the base class for an entire family of exception classes, declared in java.langpackage as java.lang.Throwable. A throwable contains a snapshot of the execution stack at the time it was created and also a message string that gives more information about the error. This class can be instantiated and thrown by the program. The throwable class is further divided into two subclasses :-
- Exceptions - Exceptions are thrown if any kind of unusual condition occurs that can be caught. Sometimes it also happens that the exception could not be caught and the program may get terminated. Remember that they are a member of Exception family and can be type ofChecked or Unchecked exception.
- Errors - When any kind of serious problem occurs which could not be handled easily likeOutOfMemoryError then an error is thrown. Well, errors are not something which is thrown by you rather they are thrown by the Java API or by the Java virtual machine itself i.e. only the exceptions are thrown by your code and not the errors. Also Remember that they are a member of Error family.
The exception classes can be explained as well seeing the exception hierarchy structure:
The java.lang package defines several classes and exceptions. Some of these classes are not checked while some other classes are checked.
EXCEPTIONS | DESCRIPTION | CHECKED | UNCHECKED |
ArithmeticException | Arithmetic errors such as a divide by zero | - | YES |
ArrayIndexOutOfBoundsException | Arrays index is not within array.length | - | YES |
ClassNotFoundException | Related Class not found | YES | - |
IOException | InputOuput field not found | YES | - |
IllegalArgumentException | Illegal argument when calling a method | - | YES |
InterruptedException | One thread has been interrupted by another thread | YES | - |
NoSuchMethodException | Nonexistent method | YES | - |
NullPointerException | Invalid use of null reference | - | YES |
NumberFormatException | Invalid string for conversion to number | - | YES |
As you have come to know that exceptions are Objects that means an object is thrown when you throw an exception. Moreover only those objects could be thrown whose classes are derived fromThrowable.
It is interesting to note here that the objects of your own design could also be thrown provided that they should be the subclass of some member of the Throwable family. Also the throwable classes which are defined by you must extend Exception class.
It depends upon the situation that whether to use an existing exception class from java.lang or create any of your own. Such as IllegalArgumentException, a subclass of RuntimeException in java.lang can be thrown if any method with an invalid argument is thrown by you. On the other hand you need not to worry if you wish to impart some more information about any unusual condition other than a class from java.lang because it will be indicated by the class of exception object itself.
For example, if a thrown exception object has class IllegalArgumentException, that indicates someone passed an illegal argument to a method. Sometimes you will want to indicate that a method encountered an abnormal condition that isn't represented by a class in the Throwable family of java.lang.
For instance, lets tweak an example below that demonstrates the exceptional conditions that might occur while driving a car.
For instance, lets tweak an example below that demonstrates the exceptional conditions that might occur while driving a car.
// In Source Packet in file except/ex1/SpeedException.java class SpeedException extends Exception { } // In Source Packet in file except/ex1/VeryFastException.java class VeryFastException extends SpeedException { } // In Source Packet in file except/ex1/VerySlowException.java class VerySlowException extends SpeedException { } |
Lets tweak the diagram below.
It is clear from the above program that there is something abnormal with the speed of the car i.e. either it is very fast or it is very slow. Hence two exceptions are thrown by the program - VeryFastExceptionand VerySlowException. To be more precise the SpeedException family specifies three new exceptions thrown by the program which indicate some abnormal conditions. That is the SpeedException specifies that there is something unusual with the speed; VeryFastException and VerySlowException specifies the abnormal conditions of the speed.
NOTE: The SpeedException extends Exception only and not the Throwable or Error class.The possible exceptions in a Java program are organized in a hierarchy of exception classes. The Throwable class, which is an immediate subclass of Object, is at the root of the exception hierarchy.Throwable has two immediate subclasses: Exception and Error. Figure 9.1 shows the standard exception classes defined in the java.lang package, while Figure 9.2 shows the standard error classes defined in java.lang.
Exceptions
All of the subclasses of Exception represent exceptional conditions that a normal Java program may want to handle. Many of the standard exceptions are also subclasses of RuntimeException. Runtime exceptions represent runtime conditions that can generally occur in any Java method, so a method is not required to declare that it throws any of the runtime exceptions. However, if a method can throw any of the other standard exceptions, it must declare them in its throws clause.
A Java program should try to handle all of the standard exception classes, since they represent routine abnormal conditions that should be anticipated and caught to prevent program termination.
Runtime exceptions
The java.lang package defines the following standard runtime exception classes:
- ArithmeticException
- This exception is thrown to indicate an exceptional arithmetic condition, such as integer division by zero.
- ArrayIndexOutOfBoundsException
- This exception is thrown when an out-of-range index is detected by an array object. An out-of-range index occurs when the index is less than zero or greater than or equal to the size of the array.
- ArrayStoreException
- This exception is thrown when there is an attempt to store a value in an array element that is incompatible with the type of the array.
- ClassCastException
- This exception is thrown when there is an attempt to cast a reference to an object to an inappropriate type.
- IllegalArgumentException
- This exception is thrown to indicate that an illegal argument has been passed to a method.
- IllegalMonitorStateException
- This exception is thrown when an object's wait(), notify(), or notifyAll() method is called from a thread that does not own the object's monitor.
- IllegalStateException
- This exception is thrown to indicate that a method has been invoked when the run-time environment is in an inappropriate state for the requested operation. This exception is new in Java 1.1.
- IllegalThreadStateException
- This exception is thrown to indicate an attempt to perform an operation on a thread that is not legal for the thread's current state, such as attempting to resume a dead thread.
- IndexOutOfBoundsException
- The appropriate subclass of this exception (i.e., ArrayIndexOutOfBoundsException or StringIndexOutOfBoundsException) is thrown when an array or string index is out of bounds.
- NegativeArraySizeException
- This exception is thrown in response to an attempt to create an array with a negative size.
- NullPointerException
- This exception is thrown when there is an attempt to access an object through a null object reference. This can occur when there is an attempt to access an instance variable or call a method through anull object or when there is an attempt to subscript an array with a null object.
- NumberFormatException
- This exception is thrown to indicate that an attempt to parse numeric information in a string has failed.
- RuntimeException
- The appropriate subclass of this exception is thrown in response to a runtime error detected at the virtual machine level. Because these exceptions are so common, methods that can throw objects that are instances of RuntimeException or one of its subclasses are not required to declare that fact in their throws clauses.
- SecurityException
- This exception is thrown in response to an attempt to perform an operation that violates the security policy implemented by the installed SecurityManager object.
- StringIndexOutOfBoundsException
- This exception is thrown when a String or StringBuffer object detects an out-of-range index. An out-of-range index occurs when the index is less than zero or greater than or equal to the length of the string.
Other exceptions
The java.lang package defines the following standard exception classes that are not runtime exceptions:
- ClassNotFoundException
- This exception is thrown to indicate that a class that is to be loaded cannot be found.
- CloneNotSupportedException
- This exception is thrown when the clone() method has been called for an object that does not implement the Cloneable interface and thus cannot be cloned.
- Exception
- The appropriate subclass of this exception is thrown in response to an error detected at the virtual machine level. If a program defines its own exception classes, they should be subclasses of theException class.
- IllegalAccessException
- This exception is thrown when a program tries to dynamically load a class (i.e., uses the forName() method of the Class class, or the findSystemClass() or the loadClass() method of theClassLoader class) and the currently executing method does not have access to the specified class because it is in another package and not public. This exception is also thrown when a program tries to create an instance of a class (i.e., uses the newInstance() method of the Class class) that does not have a zero-argument constructor accessible to the caller.
- InstantiationException
- This exception is thrown in response to an attempt to instantiate an abstract class or an interface using the newInstance() method of the Class class.
- InterruptedException
- This exception is thrown to signal that a thread that is sleeping, waiting, or otherwise paused has been interrupted by another thread.
- NoSuchFieldException
- This exception is thrown when a specified variable cannot be found. This exception is new in Java 1.1.
- NoSuchMethodException
- This exception is thrown when a specified method cannot be found.
Errors
The subclasses of Error represent errors that are normally thrown by the class loader, the virtual machine, or other support code. Application-specific code should not normally throw any of these standard error classes. If a method does throw an Error class or any of its subclasses, the method is not required to declare that fact in its throws clause.
A Java program should not try to handle the standard error classes. Most of these error classes represent non-recoverable errors and as such, they cause the Java runtime system to print an error message and terminate program execution.
The java.lang package defines the following standard error classes:
- AbstractMethodError
- This error is thrown in response to an attempt to invoke an abstract method.
- ClassCircularityError
- This error is thrown when a circular reference among classes is detected during class initialization.
- ClassFormatError
- This error is thrown when an error is detected in the format of a file that contains a class definition.
- Error
- The appropriate subclass of this error is thrown when an unpredictable error, such as running out of memory, occurs. Because of the unpredictable nature of these errors, methods that can throw objects that are instances of Error or one of its subclasses are not required to declare that fact in their throws clauses.
- ExceptionInInitializerError
- This error is thrown when an unexpected exception is thrown in a static initializer. This error is new in Java 1.1.
- IllegalAccessError
- This error is thrown when a class attempts to access a field or call a method it does not have access to. Usually this error is caught by the compiler; this error can occur at run-time if the definition of a class changes after the class that references it was last compiled.
- IncompatibleClassChangeError
- This error or one of its subclasses is thrown when a class refers to another class in an incompatible way. This situation occurs when the current definition of the referenced class is incompatible with the definition of the class that was found when the referring class was compiled. For example, say class A refers to a method in class B. Then, after class A is compiled, the method is removed from class B. When class A is loaded, the run-time system discovers that the method in class B no longer exists and throws an error.
- InstantiationError
- This error is thrown in response to an attempt to instantiate an abstract class or an interface. Usually this error is caught by the compiler; this error can occur at run-time if the definition of a class is changed after the class that references it was last compiled.
- InternalError
- This error is thrown to signal an internal error within the virtual machine.
- LinkageError
- The appropriate subclass of this error is thrown when there is a problem resolving a reference to a class. Reasons for this may include a difficulty in finding the definition of the class or an incompatibility between the current definition and the expected definition of the class.
- NoClassDefFoundError
- This error is thrown when the definition of a class cannot be found.
- NoSuchFieldError
- This error is thrown in response to an attempt to reference an instance or class variable that is not defined in the current definition of a class. Usually this error is caught by the compiler; this error can occur at run-time if the definition of a class is changed after the class that references it was last compiled.
- NoSuchMethodError
- This error is thrown in response to an attempt to reference a method that is not defined in the current definition of a class. Usually this error is caught by the compiler; this error can occur at run-time if the definition of a class is changed after the class that references it was last compiled.
- OutOfMemoryError
- This error is thrown when an attempt to allocate memory fails.
- StackOverflowError
- This error is thrown when a stack overflow error occurs within the virtual machine.
- ThreadDeath
- This error is thrown by the stop() method of a Thread object to kill the thread. Catching ThreadDeath objects is not recommended. If it is necessary to catch a ThreadDeath object, it is important to re-throw the object so that it is possible to cleanly stop the catching thread.
- UnknownError
- This error is thrown when an error of unknown origins is detected in the run-time system.
- UnsatisfiedLinkError
- This error is thrown when the implementation of a native method cannot be found.
- VerifyError
- This error is thrown when the byte-code verifier detects that a class file, though well-formed, contains some sort of internal inconsistency or security problem.
- VirtualMachineError
- The appropriate subclass of this error is thrown to indicate that the Java virtual machine has encountered an error.
I am sure this article has touched all the internet
ReplyDeletevisitors, its really really pleasant piece of writing on building up new
weblog.
Visit my web site :: facebook fans
You likely listened to Insta Followers sometime recently. We are giving social media development items for people and brands. Purchase from India's No.-1 advancing site https://www.ytviews.in/ with 24/7 clients back and ensure.
ReplyDelete