Results 1 to 2 of 2
  1. #1
    Java Example is offline Moderator
    Join Date
    Sep 2014
    Posts
    27
    Rep Power
    0

    Default What is chained Exception in java programs?

    In this tutorial, we will discuss about the chained exception. Chained exception occurs whenever in a program the first exception causes an another exception. Java provides new functionality for chaining exceptions. Exception chaining (also known as "nesting exception") is a technique for handling the exception, which occur one after another i.e. most of the time is given by an application to response to an exception by throwing another exception. Typically the second exception is caused by the first exception. Therefore chained exceptions help the programmer to know when one exception causes another.

    The constructors that support chained exceptions in Throwable class are:
    Throwable initCause(Throwable)
    Throwable(Throwable)
    Throwable(String, Throwable)
    Throwable getCause()



    Java Code: Chained Exception example
    import java.io.*;
    import java.util.*;
    
    class MyException extends Exception {
    
        MyException(String msg) {
            super(msg);
        }
    }
    
    public class ChainExcep {
    
        public static void main(String args[]) throws MyException, IOException {
            try {
                int rs = 10 / 0;
            } catch (Exception e) {
                System.err.println(e.getMessage());
                System.err.println(e.getCause());
                throw new MyException("Chained ArithmeticException");
            }
        }
    }
    Output of the Program:

    What is chained Exception in java programs?-chained-exception.jpg
    Figure: Chained exception example


    This example has an user defined exception that throws an ArithmeticException and has been thrown under the catch handler. After throwing an exception, the handler will execute the statement of the catch block and then invoke the user defined constructor. Thus the implementation of chained exception is very helpful to the user to make a program or an application error and exception free.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: What is chained Exception in java programs?

    New functionality? Don't talk nonsense; it has been here since Java 1.1

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Run Java Programs
    By StevenR in forum New To Java
    Replies: 2
    Last Post: 02-20-2013, 09:33 AM
  2. Replies: 4
    Last Post: 01-09-2013, 02:05 PM
  3. Java Network Programs
    By sixxvirus in forum New To Java
    Replies: 5
    Last Post: 03-04-2012, 08:00 AM
  4. chained exception
    By simorgh in forum New To Java
    Replies: 15
    Last Post: 08-14-2010, 04:13 PM
  5. How to use chained exceptions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-04-2008, 02:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •