In this article we will show you the solution of exception class hierarchy in java, an exception in Java is defined as "an occurrence that occurs during the execution of a programme that disturbs the normal flow of instructions."
This is typically an unanticipated or undesirable occurrence that might happen in application code during compile-time or run-time.
There are various sorts of Java exceptions, and each exception type is arranged in a basic hierarchy.
Step By Step Guide On Exception Class Hierarchy In Java :-
The Throwable class which is a direct subclass of the Object class, is the class placed at the top of the hierarchy of exception classes.
Exception and Error are the two direct subclasses of Throwable class.
1. Throwable class - It is the top class in the exception hierarchy, as depicted in the above diagram, and is descended from Object class, from which all other exception classes are either directly or indirectly derived.
The source of all exception classes is it. The java.lang package contains it.
The superclass of all exceptions in Java is called the Throwable class. Error and Exception are the two subclasses of this class. Java programmes' exceptions or errors are objects of these types.
Additionally, you can design your own unique exceptions using the Throwable class.
2. Error - The Throwable class's subclass, Error, serves as the superclass for all runtime error classes. If a system or resource-related issue arises, it ends the programme (JVM).
An error typically denotes an exceptional issue or circumstance that is challenging to resolve. It does not result from programming errors. It typically happens when a system is malfunctioning or when resources are not being allocated effectively.
Examples of errors include VirtualMachineError, StackOverFlowError, AssertionError, LinkageError, OutOfMemoryError, etc. In subsequent tutorials, we shall learn more specifically.
3. Exception - It is represented by an Exception class, which depicts faults brought on by both internal and external variables in a programme. Throwable class's subclass, the exception class, is the superclass of all exception classes.
The Exception class is the immediate or indirect ancestor of all exception classes. They come from the application itself.
Custom Exceptions - According to their own requirements, users or programmers can define custom exceptions.
The user-defined exceptions, also known as custom exceptions, are made by extending the exception class.
Therefore, Java gives programmers the freedom to handle errors while addressing the functional requirements of the issues they are tackling.
In later tutorials, we'll go into more detail about custom exceptions.
Let's look at a brief summary of each Exception class subclass.
Class RuntimeException (Unchecked Exceptions)
The Exception class has a subclass called RuntimeException.
When an arithmetic operation carried out by the programme is flawed or a problem or defect appears in the program's code, the JVM or programmatically will throw this exception.
class MyException extends Exception { public MyException(String s) { } } public class Main { // Driver Program public static void main(String args[]) { try { throw new MyException("Talkerscode.com"); } catch (MyException ex) { System.out.println("Caught"); System.out.println(ex.getMessage()); } } }
- Creating class that represents use defined exception
- Next a class that uses above MyException
- Main method i.e. public static void main(String args[])
- Throws an object of user defined exception that will print Talkerscode.com
- Catching the exception and printing caught.
- Lastly will print System.out.println(ex.getMessage());
- Run the code
Conclusion :-
Here we learned about Exception Hierarchy class throwable in Java with the help of a diagram and an example program of custom exception.
In the next tutorials we will learn more about some exceptions in detail.
I hope this article on exception class hierarchy in java helps you and the steps and method mentioned above are easy to follow and implement.