 Hello friends, I am Sanjay Gupta. In this video I am going to demonstrate you the use of Chained Exception in Java. Before starting, you can subscribe my YouTube channel by clicking on the link which is available at the bottom right corner of this video. You can download my app Takimas that is available on Google Play. You can connect with me by following these details which are available in front of you. Now coming on to the topic, first we have to understand it theoretically. So first point says sometimes a catch block carries one exception type then throws a new exception of a different type to indicate that a program specific exception occurred. In earlier Java versions there was no mechanism to wrap the original exception information with the new exceptions information to provide a complete stacked trace showing where the original problem occurred in the program. This made debugging such problems particularly difficult. So J2SE 1.4 added chained exceptions to enable an exception object to maintain the complete stacked trace information. So in case of chained exception more than one exception will be thrown one by one by several methods. So we can understand this concept with the help of an example. So you can see the example which is available here in front of you. Here a class test is implemented. Inside this test class main method is defined which is having try and catch. Inside try I am calling method one. So method one is called here and after try catch block is defined. While method one is calling, its control will be transferred to its definition. So inside method one definition you can see it is implementing through clause which is throwing an exception of exception class and inside its definition try and catch are defined. Try is calling method two. So method one is calling method two and if any exception is raised inside method one that will be thrown and that will be handled by the caller of this method one which is main and here when method two is called control will be transferred to method two definition. So inside method two definition try and catch are defined. Here try is calling method three and method two header declaration is throwing an exception for exception class. So if any exception is raised inside method two that will be handled by method one because method one is caller of method two. That's why method one is containing this catch block. So this catch block is to handle the exceptions which are raised by a method two. Similar to method one, method two is calling method three. This is the definition of method three. It is again throwing an exception of exception class. So here throws clause is available. So now after calling method three, no other methods are called. So this method three will throw an object of exception class using through keyword and this message will be thrown method three is called by method two. So this except exception will be handled by method two. So here it is handled. It is again throwing an exception and it is merging exception thrown in method two plus this exception. This exception is an object which is having this message, which is thrown by method three. So method two and method three. These two strings are merged here and new object which is thrown with the help of this throw keyword. So this throw keyword throws an object which is handled by a method one. So here this exception object will be receiving the message, which is the combination of method two plus method three. Now it will be concatenated with method one information. So here exception thrown in method one plus this exception object which is containing exception thrown in method two plus exception thrown in method three message. So all three messages are combined here and new object will be thrown to main and that object will be received here and exception dot print stack trace method will be will be displaying all the messages that are generated by method two method three and method one catch blocks. So now I am executing this code so that you can understand the output. You can see the output. First java lang java dot lang dot exception exception thrown in method one is displayed here then exception thrown in method two and then exception thrown in method three. So you can see all three messages are displayed here with the help of this print stack trace method. So friends first main is calling method one. Method one is calling method two. Method two is calling method three and method three is last method which is called by method two. It is throwing an exception which is handled by method two. Method two is throwing an exception in catch block which is handled by method one catch block. It is also throwing an exception which is handled by main catch block. So at last main catch block is calling print stack trace method and all the information is available in this exception object, which is displayed in output, which I have shown you. You can see it again. So friends, you can call multiple methods and you can throw multiple objects created by a new keyword to catch blocks and all the information will be stored in a single object and that single object can be displayed with the help of print stack trace method so that you can see the output of all the catch blocks created in this object. So you can implement this concept of chain exception in your system so that you can understand the concept well. If you want to watch more programming related videos, you can subscribe my YouTube channel. Thank you for watching this video.