×

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

Related Topics

Java Boolean logicalXor() Method

The logicalXor() method of Java Boolean class returns the result of implementing logical XOR operation on the specified Boolean operands. Syntax: public static boolean logicalXor (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

Read the XLSX file in Java

Excel files contain cells; reading an excel file in Java differs from reading a word file. JDK doesn't have a direct API that can read or write Word or Excel...

3 minutes read.

Package Program in Java

Package Program in Java The package program in Java helps us to understand the significance of packages in Java. Packages are mainly used to group together similar classes, interfaces and sub-packages....

6 minutes read.

Hierarchy of operators in Java

Operators are the most frequently used terminology in any area of programming, and it helps in various approaches to efficiently solve daily life problems by computer programming. The simple addition of...

4 minutes read.

Java Math floorMod() Method

The floorMod() method of Math class returns the floor modulus of the specified arguments. It firstly divides the dividend and divisor and then returns an integer that is equal to...

1 minute read.

Java Integer shortValue() method

The shortValue() method of Java Integer class returns a short value for this Integer after a narrowing primitive conversion. Syntax public short shortValue () Parameters NA Overrrides: The shortValue () overrides : shortValue in class Number  Return Value This...

1 minute read.

Why are generics used in Java

Java has a feature called generics that allows you to make a class, interface, and function accepting any (reference) type as a parameter. In other words, it is the idea...

4 minutes read.

Java Integer signum() method

The signum() method of Java Integer class returns the signum function of the specified int value. Syntax public static int signum (int i)  Parameters The parameter ‘i’ represents the value whose signum is to...

1 minute read.

Java Integer rotateRight() method

The rotateRight() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value right by the specified number of bits. Syntax public...

1 minute read.

Java Create File

A File is a hypothetical way, which has no genuine presence. It is very much like while "using" that File that the major real activity of putting away something in...

5 minutes read.

HttpURLConnection

HttpURLConnection Protocol It is a standard set of rules that allow electronic devices to communicate with each other. The http protocol The http protocol is for data communication, distributing, collaborating, hypermedia information systems, on the World Wide...

5 minutes read.

Java File Input Stream

Java makes use of stream ideas to speed up input and output processes. All packages of input and output streams are contained in java.io.package. Stream: A stream is nothing more...

5 minutes read.

Deque in Java

Deque in java collections with Example Deque is short for “double-ended queue.” It is a linear collection that extends the Queue interface and supports insertion and deletion of the element at both the...

3 minutes read.

this keyword in Java

The 'this' keyword is an essential notion in Java. We'll go through the 'this' keyword in depth and provide some examples of how it's used in Java. In Java, the term...

5 minutes read.

How to Import Packages in Java

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Java LinkedHashMap

LinkedHashMap implements the Map interface. It inherits the HashMap class. Some essential features of LinkedHashMap are: It contains the values based on the keys. It maintains the order of insertion. It contains unique...

5 minutes read.

Java Xmx

This section will explain what Xmx in Java is and how to establish a Java application's maximum heap size. When we execute a Java application, it occasionally displays an error message...

3 minutes read.

Java Integer sum() method

The sum() method of Java Integer class add the two specified integers values. It returns the same result as given by + operator. Syntax public static int sum (int a, int b)  Parameters The...

1 minute read.

Java Math ulp() Method

The ulp() method of Java Math class returns the size of an ulp of the argument. Syntax: public static double ulp(double x)public static float ulp(float x) Parameters: The parameter ‘x’ represents the floating-point number...

2 minutes read.

Java String format() method

format() method returns a formatted String based on the given locale,specified format and arguments. Syntax: public static String format(String format , Object… args) Parameter: locale : It specifies locale value to be applied on...

2 minutes read.