Error and Exceptions

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. An Error is represented by an object of class Error. But an error is too severe for a program to handle mostly abnormal conditions, the program must stop running.
Example :
  • AnnotationFormatError – Thrown when the annotation parser attempts to read an annotation from a class file and determines that the annotation is malformed.
  • AssertionError – Thrown to indicate that an assertion has failed.
  • LinkageError – Subclasses of LinkageError indicate that a class has some dependency on another class; however, the latter class has incompatibly changed after the compilation of the former class.
  • VirtualMachineError – Thrown to indicate that the Java Virtual Machine is broken or has run out of resources necessary for it to continue operating.
An Exception is a problem that is caused by the circumstances outside the  control of the program ,such as bad user input. When an exception occurs, the JVM  creates an object of an class Exception which holds the information about the problem. A Java program itself may catch an exception and can then use Exception object to recover from the problem. 
Example : NumberFormatException , RuntimeException…
 
throwableHierarchyA
 
Java Docs :Error Exception
Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown. eg, IOException thrown by java.io.FileInputStream’s read() method.
    With an unchecked exception, however, the compiler doesn’t force client programmers either to catch theexception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsException thrown by String’s charAt() method·
 
Error along with RuntimeException & their subclasses are unchecked exceptions. All other Exception classes are checked exceptions.
 
Checked and Unchecked Exceptions
 S.No.  Checked Exceptions  Unchecked Exceptions
 1. Any subclass of Exception(or Exception itself),excluding class RuntimeException and its subclasses  Any subclass of RuntimeException or Error and its subclasses
 2.  Must be caught at compile time using try-catch-finally block  need not to be handled at compile time.
 3. Checked exceptions are generally those from which a program can recover & it might be a good idea to recover from such exceptions programmatically These are those exceptions that might not happen if everything is in order, but they do occur
 4.  SQLException,IOException  NullPointerException ,OutofMemroyError
Rate this post

Leave a Reply