×

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

Convert List to String in Java

We occasionally need to create a string from a list of characters. Since a string is only a collection of characters, a character array can be converted into a string....

6 minutes read.

Armstrong Number Program in Java

Armstrong Number Program in Java: A positive number is called an Armstrong number if the sum of the cube of each digit is equal to the number itself. There are...

6 minutes read.

Java Constant

A constant is an unchangeable entity in coding, as its title implies. The value which cannot be altered, in other terms. We shall understand about Java constants and exactly how...

3 minutes read.

Operators in Java

There is a good operator environment provided by Java. These operators are divided into four different groups i.e. arithmetic, bitwise, relational, and logical. There are other operators also available to...

7 minutes read.

ArrayDeque in Java

ArrayDeque The ArrayDeque is one of the essential concepts in java to implement the deque interface. It will allow us to apply a resizable array to implement the Deque interface. This...

8 minutes read.

Swastika Pattern in Java

This part teaches us how to create the Swastika Pattern in Java utilizing user-defined columns and rows as well as stars or other special characters.A Java pattern application that is...

2 minutes read.

How to override toString() method in Java?

Java is an object-oriented language. It only works with classes and objects. Thus, whenever we need to calculate, we need an object or objects that belong to the class. The Java method...

2 minutes read.

Various operations on HashSet in Java

In this article, you will be acknowledged about what is a HashSet in java and what are its operations in java programming language. The HashSet is a crucial part of...

3 minutes read.

Encapsulation Program in Java

Encapsulation Program in Java Encapsulation program in Java demonstrates the technique to bind methods and fields in a single unit. The term encapsulation is inspired by the word ‘capsule’, which is...

3 minutes read.

Java Protected Keyword

An access modifier is a keyword that Java protects. It can be used to constructors, methods, inner classes, and variables. Variables, methods, and constructors that have been marked protected in...

3 minutes read.

Checked vs Unchecked Exceptions in Java

In this tutorial, we will discuss Java's checked and unchecked Exceptions. Exception An exception is an undesirable event that disrupts the normal flow of the program. An exception is thrown at runtime. It...

4 minutes read.

Functional Interface in Java 8

A brief introduction to Interface in Java: Interfaces in Java are basically the blue print of classes. Before the appearance of Java 8, it was only possible to declare one or...

11 minutes read.

Non-primitive data types in Java

The kind of data stored in the variable is determined by its type. The type describes the data category (different sizes and values). These are not already built into the devices....

4 minutes read.

Convert JSON to Map in Java

JACKSON and GSON libraries are two extremely capable JSON-related libraries offered by Java. To efficiently work with the returned JSON data, we frequently need to convert JSON responses into a...

6 minutes read.

Java Subtract Days from Current Date

Dealing with date and time in Java is not a particularly challenging operation because Java has an API for date and time that simplifies duties for developers. There are two...

3 minutes read.

User Defined Custom Exceptions in Java

In this tutorial, we will discuss user-defined custom exceptions with examples. Introduction In Java, we have proactively characterised, Exception classes, for example, ArithmeticException, NullPointerException, ArrayoutOfBound and so on. These built-in exceptions are...

3 minutes read.

Java Enhanced For Loop (For-each Loop)

JAVA ENHANCED FOR LOOP The enhanced for loop is also called the for-each loop and has some advantages over the regular for loop. A for-each loop is designed to cycle through...

4 minutes read.

How to install Java in Windows 10

To make programs that can run on our systems, we need to install the programming language related software in our systems. Different programming language requires a different type of software aka...

6 minutes read.

Split String into String Array in Java

The String split() technique returns a variety of divided strings after the strategy parts the given String around matches of a given normal articulation containing the delimiters. The ordinary articulation...

4 minutes read.

Display Unique Rows in a Binary Matrix in Java

To solve this problem, we must first locate and display the distinct rows of a supplied binary matrix afterward. We will go through how to show distinct rows inside a...

12 minutes read.