0% found this document useful (0 votes)
49 views4 pages

Packages: Exception Hierarchy

The Throwable class is the superclass for all errors and exceptions in Java. It has two main subclasses: Exception and Error. Exception represents potential problems that occur during execution due to programmer error or other causes that can potentially be recovered from. Error represents problems that typically occur due to resource deficiencies and are difficult to recover from. Exceptions are further broken down into checked exceptions, which must be declared or handled, and unchecked exceptions, which do not require declaration or handling but indicate serious problems.

Uploaded by

suraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views4 pages

Packages: Exception Hierarchy

The Throwable class is the superclass for all errors and exceptions in Java. It has two main subclasses: Exception and Error. Exception represents potential problems that occur during execution due to programmer error or other causes that can potentially be recovered from. Error represents problems that typically occur due to resource deficiencies and are difficult to recover from. Exceptions are further broken down into checked exceptions, which must be declared or handled, and unchecked exceptions, which do not require declaration or handling but indicate serious problems.

Uploaded by

suraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Packages

1.java.lang

INTERFACES

1. public interface AutoCloseable


An object that may hold resources (such as file or socket handles) until it is closed. The close() method of
an AutoCloseable object is called automatically when exiting a try-with-resources block for which the object has been
declared in the resource specification header. This construction ensures prompt release, avoiding resource exhaustion
exceptions and errors that may otherwise occur.

CLASSES

1. public class Throwable


extends Object
implements Serializable

The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances
of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the
Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause. For the
purposes of compile-time checking of exceptions, Throwable and any subclass of Throwable that is not also a subclass of
either RuntimeException or Error are regarded as checked exceptions.

2. Exception Hierarchy:
Throwable class is the root class for every exception and it branches out to 2 main categories i.e.;

1. Exception
2. Error
java.lang.Throwable:
 Throwable is the root class for exception & it’s sub-type and error & it’s sub-types
 In other words, it is super class for exception & error
 java.lang.Throwable class extends java.lang.Object class (as shown in the above figure)
 It defines 2 sub classes i.e.; Exception and Error

java.lang.Exception:
 java.lang.Exception is super class for all types of Exception
 It extends java.lang.Throwable class
 Exception are due to  programmatic logic
 And it is recoverable
 Exception are categorized into checked exception and unchecked exception
 Example: RuntimeException, SQLException, IOException, FileNotFoundException, ArithmeticException,
NullPointerException

java.lang.Error:
 java.lang.Error is super class for all types of Error
 It extends java.lang.Throwable class
 Error are due to lack of system resources
 And it is non-recoverable
 All error fall into unchecked exception category, as it is raised due to lack of system resources at runtime
 It is out of programming scope as such type of error can’t predicted, may be well planned care can be taken to
avoid these kind of Error
 Example: VirtualMachineError, AssertionError, ExceptionInInitializerError, StackOverflowError,
OutOfMemoryError, LinkageError, InstantiationError
Note: above mentioned Exception and Error are again categorized into checked and unchecked exceptions
Checked Exception v/s Unchecked
Exception
In this article, we will discuss checked and unchecked exception in detail with explanation & examples and also list some of
most commonly known checked & unchecked exception

Checked Exception:
 Exception which are checked at compile-time during compilation is known as Checked Exception
 Alternate definition: any line of code that could possibly throw exception, and if it is raised to handle
during compilation is said to be checked exception
 For example, accessing a file from remote location could possibly throw file not found exception
 It is the programmer’s responsibility to handle the checked exception for successful compilation
 This way, if any exception is raised during execution then respective handling code will be executed
 Note: if it isn’t handled then program will throw compile-time error
 Example: IOException, FileNotFoundException, InterruptedException, SQLException, etc
 Except Runtime exception & its child classes and error & its child classes, all other exception falls under the
category of Checked Exception

Possible solution:
 Provide/surround with try-catch block or
 add throws clause in method signature at the end
 

Unchecked Exception:
 Exception which are NOT checked at compile-time is known as Unchecked Exception
 Alternate definition: any line of code that could possibly throw exception at runtime is said to be unchecked
exception
 Unchecked exception are because of programming-error
 For example, accessing out of index-position to assign some value during execution could possibly throw exception
at runtime
 So, it is again programmer’s responsibility to handle unchecked exception by providing alternate solution in the
exception handling code
 Note: if it isn’t handled properly then program will terminate abnormally at runtime
 Example: Runtime exception & its child classes and error & its child classes are examples of Unchecked Exception
 Like ArithmeticException,
 NullPointerException,
 NumberFormatException,
 ArrayIndexOutOfBoundsException,
 StatckOverflowError, etc
Misconception about checked and unchecked exception:
 Sometimes, checked exception are also referred as compile-time exception and unchecked exception are referred
as runtime exception
 But this is mis-leading because every exception (whether it is checked or unchecked) occurs/raised only at the
runtime i.e.; during program execution only
 Reason: during compilation; checked exception are caught and raises compile-time error, due to which programmer
has to handle the exception by providing either try-catch blocks or using throws keyword
 Whereas unchecked exception aren’t caught during compilation, rather it raises exception during execution
because of programming error
 

You might also like