 Hello friends, I am Sanjay Gupta. In this video I am going to demonstrate how you can throw a user defined exception using throw keyword. Before starting you can note my information, you can subscribe my YouTube channel by clicking on the link which is available at bottom right corner of this video. You can download my app Takimiz that is available on Google Play. You can connect with me by following these details which are in front of you. Now coming on to the topic, first I am going to explain you what is throw statement in Java. Then I will be demonstrating an example with you. So first statement says you have been catching exceptions that are thrown by the Java runtime system. However, it is possible for your program to throw an exception explicitly using the throw statement. So these explicit exceptions will be created by the user by defining user defined classes. The general form of throw is shown here. So you have to use a keyword throw and then throwable instance. So this is the syntax for throw keyword that you have to use. Next point says here throwable instance must be an object of type throwable or a subclass of throwable. Then simple types such as int or cal as well as non throwable classes such as string and object cannot be used as exceptions. So this you have to take care. There are two ways you can obtain or throw throwable object using a parameter into a catch clause or by creating one with a new operator. So this is brief about throw statement. Now I am going to demonstrate you the example so that you can understand how we can throw a user defined exception with the help of throw keyword. And here in this example I will be using throws clause which I have demonstrated in my previous video which is available in this playlist. So here you can see first class which I have implemented is invalid roll number exception and it extends java.length.exception which is a subclass of throwable that you have to inherit because without inheriting this class you cannot throw object of this class as an exception. Then this is constructor which will receive message in this variable which is of string type and with the help of super keyword this message will be passed to the super class of this invalid roll number exception that is exception class. Second class is student. Inside student class name and roll number two attributes are declared. One is for string type and one for integer type. Then this setData method is defined with throws clause. So here if any exception will be created that will not be handled by this setData method it will be handled by the caller of this method because it is specifying throws clause and this method can throw invalid roll number exception. Here you can see if condition is available which is checking r greater than 0. So if roll number is greater than 0 then this if condition will be true and n and r will be assigned into roll number or name. If this condition is false it means if roll number is 0 or less than 0 then an object of invalid roll number exception will be created and its name is e. This object will be created and message roll number cannot be negative. This message will be passed inside the constructor of this class. Then throw keyword is responsible for throwing this object e. Whenever we use throw keyword then forcefully we have to define throws clause. If you are defining throws clause then only you can throw an object of that class which is available with throws clause. So this is setData method. After that show method is defined which is displaying name and roll number with the help of this system.out.println. So this is the complete definition of student class. Now third class is test. Inside test class you can see main method is defined. Main is creating an object of student class that is st. Inside try st.setData statement is written. So you can see main method is responsible for calling setData method and inside setData James and minus 46 is passed. So minus 46 is a negative value. Whenever we are calling setData method control will be transferred to its definition. Here setData method is defined. So if this condition is false right now it is false. So control will be transferred inside else then an object of invalid roll number exception will be created and it will be thrown by throw keyword and control will be transferred to this catch. So catch will receive that thrown object inside e and this message will be displayed on console. You can also print e on console and you can write your own message in this catch block. After try and catch st.show is available here. So if exception is raised then also this show method will be executed. If exception is not raised then also this show method will be executed. So now I am going to run this code. You can see the output the roll number cannot be negative. This is displayed with the help of try and catch then student name null and roll number is zero because values are not assigned inside these attributes. That's why these default values are printed here. You can see that even after raising an exception this statement is executed because we are applying exception handling in this code. Now if I remove this minus sign and again execute same code you can see student name and roll number both are displayed properly. Now in this scenario no exception hand no exception has been raised and no exception has been handled by the catch block. So friends in this demonstration set data method is throwing an exception. It is using throws clause as well as throw keyword. The exception which is thrown by set data method is handled inside main method because main is caller of set data. So here I have implemented try and catch because of try and catch some set of instruction will may be executed or may not be executed. Rest of the statement will be executed 100%. So you can implement this complete code 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.