In Java, checked exceptions are exceptions that must be either caught or declared in the method signature using the throws
keyword. These exceptions are subclasses of the Exception
class, but not of the RuntimeException
class.
The hierarchy for checked exceptions is as follows:
1. Throwable Class
-
The root of all error and exception classes in Java.
-
Throwable
has two main subclasses:-
Error
: Represents serious errors that typically cannot be handled by the program (e.g.,OutOfMemoryError
). -
Exception
: Represents all exceptions, both checked and unchecked, that can be caught and handled.
-
-
2. Exception Class
-
The superclass of all checked exceptions (excluding
RuntimeException
and its subclasses).-
Exception
is further divided into several subclasses.
-
3. Subclasses of Exception
-
IOException
:-
Represents exceptions related to Input/Output operations, such as reading from a file or network.
-
Common subclasses:
-
FileNotFoundException
-
EOFException
-
SocketException
-
UnsupportedEncodingException
-
-
-
SQLException
:-
Represents exceptions related to database access, such as errors with SQL queries.
-
Common subclasses:
-
SQLTimeoutException
-
SQLSyntaxErrorException
-
SQLDataException
-
-
-
ClassNotFoundException
:-
Thrown when an application tries to load a class via its name, but the class is not found.
-
-
InstantiationException
:-
Thrown when an application tries to instantiate an abstract class or an interface.
-
-
InterruptedException
:-
Thrown when a thread is interrupted while it’s waiting, sleeping, or otherwise occupied.
-
-
CloneNotSupportedException
:-
Thrown when an object does not support the
clone()
method.
-
-
NoSuchMethodException
:-
Thrown when a method cannot be found.
-
-
FileNotFoundException
:-
A subclass of
IOException
, thrown when a file is expected to be found, but is not.
-
-
ParseException
:-
Thrown when a parsing operation fails (e.g., parsing dates, numbers).
-
4. Other Exception Subclasses:
There are many other checked exceptions, and their exact classification depends on the context in which they occur. A few notable ones include:
-
TimeoutException
:-
Thrown when a specific operation times out.
-
-
RemoteException
:-
Thrown by remote method calls (e.g., in distributed systems).
-
-
URISyntaxException
:-
Thrown when a URI syntax is invalid.
-
Checked exceptions in Java form a critical part of robust error handling, enabling developers to handle predictable problems that occur during runtime. By understanding the hierarchy of checked exceptions, developers can decide which exceptions need to be caught and handled, ensuring a stable, user-friendly application.