In this video we are discussing user defined exceptions
In the previous video we have discussed built-in exceptions. That means those exceptions were predefined and pre-retained within the Java program
So now in case of user-defined exceptions, we can create our custom exceptions
We can write one class and the class should inherit or the class should extend the exception class
And that is the common practice while defining user-defaults. defining user defined exceptions
Programmer can create custom exceptions and they are known as the user defined exceptions
To create an exception, we need to define a class for the exception and extends it to the
exception class. As I have mentioned earlier, that if I define one class, the name of the class may be my
exception, but it is of a common practice to make the class extending exception class
so that I can define my custom exception. When we need to generate the newly created exception
we will use the throws keyword for the method and it indicates that the function can throw an exception
So in our code we'll be using throws keyword which will indicate that this respective function
can throw some exception in our code. So this is the concept
So for the better, clarity on this concept let us go for one practical demonstration where we'll be
creating user defined exceptions and we shall explaining our code into a better way let us go for a demonstration on user defined exceptions here we have defined one class the name of the class
is below age exception which extends the exception class here and this is a
constructor and this a constructor of the class below age exception and from this
constructor it is calling the best class constructor here the best class is
exception and the best class constructor has been called using super passing this argument string as input argument so age is under 18 we've defined
another class the name of the class is application where we're having this name course and age so three instance variables we're having application is
the name of the constructor which is taking this name and the course as input
argument so this name and course the instance variables are getting initialized
here age is also getting initialized with the default value of 18 this is
non parameter as constructor obviously the name of the constructor will be same as
that of the class name so the name of the constructor is application which is
non parameterized and from this non parameter as constructed we are calling
the parameter as constructor so these null string comma null string so here you
can find that this this is actually referring to this parameter as constructor
which takes two string objects as input argument and here we are passing this
to null strings here so now we are having another method that
is the set age which takes this age as input argument and which throws the below edge
exception which throws the below age exception when this input age the input argument age is less than 18 we are throwing this throw new below edge exception so you are throwing this below edge exception exception class object
so we are throwing that one so that's why this function is throwing below age
exception otherwise this age which is not less than 18 will be assigned to the
instance variable age so these dot age is equal to age now these are display details
another method we're having that is a member function so here we are printing this
name the codes the age and so on so now let us go for our class that is the
user defined exception this a main function here we have defined two objects
under the application class two objects one is the application one another one
is the application two so this is our respective and name so we have passed this
John and Java programming so this name is nothing but this is variable is
this input parameter name will get initialized with that value whatever is we are
going to pass and the course name will be initialized with this Java programming
so here this is my name and this is the course name we're having so now
application one dot display details so the display details will be printed here
so now application 2 dot set age 17 you can find that here we have passed
the age 17 which is below 18 so as a result of that these
applies this set age this particular method will throw this particular method will
throw below age exception so that's why we put this line under the tri block
and tri block is following is being followed by the catch block immediately so below edge exception e is the object of this below edge exception class and within this catch block we writing this e
dot get message and e.d. print stack stress so now let me go for the execution of
the same so if I go on executing you can find that the name of the student is
John and applied for Java programming and taking the default age 18
so now in this case what is happening so the application one dot display details it is
printing accordingly so in that next time when you are going for this set age
method will be called from this application to that is the application class
object passing this input argument as 17 so as the 17 is less than 18 so it
is throwing new below age exception so automatically the catch block will get
the control because this catch block is actually handling this below edge exception e and the respective code is getting printed so e
dot get message is printing this age is under 18 and e
dot print stack stress is printing the respective outcome. I'm going to show you the outcome here
So this outcome it is going to show. So you can find that the error has occurred at the line number 44
So you see at the line number 44 the error has occurred and line number 24 is actually throwing
this below edge. you can find that this line number 24 is here and 44 is here so it is a the
respective line numbers are getting printed so in this way in this particular
demonstration we have discussed how we can define user defined exceptions thanks for watching this video