Root exception in java
Java:
The main feature of java which is not in C or object oriented programming language is platform independence. Not only the platform independence there are many other features in java such as secure, highly interpreted, robust, portable etc. Additionally, classes and objects are used to write Java code at all times.
Exception:
An issue that develops while a program is being executed is known as an exception (or exceptional event). The usual flow of the program is interrupted when an exception occurs, and the program/application terminates unexpectedly, which is not advised. As a result, these exceptions need to be handled.
Numerous factors can lead to an exception. Here are various situations where an exception happens. A user entered incorrect information. Unable to locate a file that needs to be opened.
The JVM has run out of memory or a network connection has been lost in the midst of a communication. Others of these exceptions are brought on by user error, some by programming fault, and some by physically deficient resources.
Root Exception:
Working with nested exceptions is rather prevalent in Java since they make it easier to identify the cause of a problem. When dealing with these kinds of exceptions, there are instances when we may wish to know the underlying issue that gave rise to the exception so that our application can react appropriately in each situation.
When working with frameworks that wrap the root exceptions into their own, this is extremely helpful.
A hierarchy of exception classes is used to arrange the potential exceptions in a Java program. The base of the exception hierarchy is the Throwable class, which is an immediate subclass of Object. Exception and Error are the two immediate subclasses of Throwable.
The throwable's built-in get Cause ( ) method returns the reason of the exception or null if the cause of the exception cannot be established. The cause that was supplied by one of the constructors or that was set after creation with the init Cause ( Throwable ) method can be obtained using this method.
To ascertain the origin of the Throwable or Exception, the Print Stack Trace methods of the Throwable class call the get Cause ( ) function. It might be claimed that this method just returns the reason why the exception occurred. Now let us see the syntax to define the method get Cause ( ) as shown below.
Syntax to define the method get Cause( ):
In order to define a method or variable or any key word in any of the programming language there will be a particular syntax. So define the method get Cause ( ) the syntax is as shown below:
public Throwable get Cause ( )
Now let us see an example to find the root exception of a java program using the following code as shown below :
Example:
import java . io . *;
class Demo
{
public static void main ( String args [ ] ) throws Exception
{
try
{
division ( 15,0 );
} // try block
catch ( ArithmeticException ae )
{
System.out.println ( “ The cause of exception is : “ + ae.getCause ( ) );
} // catch block
} // main method
public static void division ( int m , int n ) throws Exception
{
try
{
int i = m / n ;
} // try block
catch ( ArithmeticException ae )
{
arithmeticException ans = new ArithmeticException ( ) ;
ans . init Cause ( ae );
throw ( ans );
} // catch block
} // division
} // demo class
Output :
Cause of Exception : java . lang . ArithmeticException : / by zero
Now let us see another example to find the root exception of a java program using the following code as shown below :
Example :
import java . io . *;
class Demo
{
public static void main ( String args [ ] ) throws Exception
{
try
{
division ( 15,0 );
} // try block
catch ( ArithmeticException ae )
{
System.out.println ( “ The cause of exception is : “ + ae.getCause ( ) );
} // catch block
} // main method
public static void division ( int m , int n ) throws Exception
{
int i = m / n ;
} // division
} // demo class
Output :
Cause of Exception : null