0% found this document useful (0 votes)
44 views3 pages

Chained Exception

The document provides three examples of chained exceptions in Java. In the first example, a NullPointerException is created and an ArithmeticException is added as the cause before throwing the NullPointerException. The second example demonstrates creating a NumberFormatException and setting a NullPointerException as the cause before throwing it. The third example shows chaining a RuntimeException caused by an ArithmeticException by catching the ArithmeticException and rethrowing a RuntimeException with the original exception set as the cause.

Uploaded by

Thendral
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views3 pages

Chained Exception

The document provides three examples of chained exceptions in Java. In the first example, a NullPointerException is created and an ArithmeticException is added as the cause before throwing the NullPointerException. The second example demonstrates creating a NumberFormatException and setting a NullPointerException as the cause before throwing it. The third example shows chaining a RuntimeException caused by an ArithmeticException by catching the ArithmeticException and rethrowing a RuntimeException with the original exception set as the cause.

Uploaded by

Thendral
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Chained Exceptions

Example 1:
class ChainExcDemo
{
static void demoproc()
{
// create an exception
NullPointerException e =new NullPointerException("top layer");
// add a cause
e.initCause(new ArithmeticException("cause"));
throw e;
}
public static void main(String args[])
{
try
{
demoproc();
} catch(NullPointerException e)
{
// display top level exception
System.out.println("Caught: " + e);
// display cause exception
System.out.println("Original cause: " +
e.getCause());
}
}
}

OUTPUT:
Caught: java.lang.NullPointerException: top layer
Original cause: java.lang.ArithmeticException: cause
2.Java program to demonstrate working of chained exceptions
public class ExceptionHandling
{
public static void main(String[] args)
{
try
{
// Creating an exception
NumberFormatException ex =new NumberFormatException("Exception");
// Setting a cause of the exception
ex.initCause(new NullPointerException("This is actual cause of the
exception"));
// Throwing an exception with cause.
throw ex;
}
catch(NumberFormatException ex)
{
// displaying the exception
System.out.println(ex);
// Getting the actual cause of the exception
System.out.println(ex.getCause());
}
}
}

OutPut:
java.lang.NumberFormatException: Exception
java.lang.NullPointerException: This is actual cause of the exception

3. Another Example Program for Chained Exception:


In Java, you can chain exceptions using the constructor of the Throwable class.
public class ExceptionExample {
public static void main(String[] args) {
try {
// code that might throw an exception
int[] numbers = new int[5];
int divisor = 0;
for (int i = 0; i < numbers.length; i++) {
int result = numbers[i] / divisor;
System.out.println(result);
}
} catch (ArithmeticException e) {
// create a new exception with the original exception as the cause
throw new RuntimeException("Error: division by zero", e);
}
}
}
In this example, an array of integers is defined and a divisor is set to 0. Inside the try block, a
for loop is used to divide each element in the array by the divisor. Since dividing by 0 is not
allowed, an ArithmeticException is thrown. This exception is caught in the catch block, which
creates a new RuntimeException object with the original ArithmeticException object as the
cause.
When you run this program, you should see the following output:
Exception in thread “main” java.lang.RuntimeException: Error: division by zero
at ExceptionExample.main(ExceptionExample.java:10)
Caused by: java.lang.ArithmeticException: / by zero
at ExceptionExample.main(ExceptionExample.java:8)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy