WEBVTT

00:00.000 --> 00:05.000
In this video we are discussing user defined exceptions

00:05.000 --> 00:15.000
In the previous video we have discussed built-in exceptions. That means
those exceptions were predefined and pre-retained within the Java program

00:15.000 --> 00:20.000
So now in case of user-defined exceptions,
we can create our custom exceptions

00:20.000 --> 00:26.000
We can write one class and the class should inherit
or the class should extend the exception class

00:26.000 --> 00:33.000
And that is the common practice while defining
user-defaults. defining user defined exceptions

00:33.000 --> 00:40.240
Programmer can create custom exceptions and
they are known as the user defined exceptions

00:40.240 --> 00:47.080
To create an exception, we need to define a
class for the exception and extends it to the

00:47.080 --> 00:53.320
exception class. As I have mentioned earlier, that if
I define one class, the name of the class may be my

00:53.320 --> 00:58.980
exception, but it is of a common practice
to make the class extending exception class

00:58.980 --> 01:06.720
so that I can define my custom exception. When
we need to generate the newly created exception

01:06.720 --> 01:15.300
we will use the throws keyword for the method and it
indicates that the function can throw an exception

01:15.300 --> 01:22.620
So in our code we'll be using throws keyword
which will indicate that this respective function

01:22.620 --> 01:27.920
can throw some exception in our
code. So this is the concept

01:27.920 --> 01:33.860
So for the better, clarity on this concept let us
go for one practical demonstration where we'll be

01:33.860 --> 01:47.055
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

01:47.055 --> 01:52.215
is below age exception which extends
the exception class here and this is a

01:52.215 --> 01:56.775
constructor and this a constructor of the
class below age exception and from this

01:56.775 --> 02:00.854
constructor it is calling the best
class constructor here the best class is

02:00.854 --> 02:11.114
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

02:11.114 --> 02:19.675
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

02:19.675 --> 02:23.834
the name of the constructor which is
taking this name and the course as input

02:23.834 --> 02:28.935
argument so this name and course the
instance variables are getting initialized

02:28.935 --> 02:33.715
here age is also getting initialized
with the default value of 18 this is

02:33.715 --> 02:38.334
non parameter as constructor obviously the
name of the constructor will be same as

02:38.334 --> 02:42.635
that of the class name so the name of
the constructor is application which is

02:42.635 --> 02:46.435
non parameterized and from this non
parameter as constructed we are calling

02:46.435 --> 02:51.595
the parameter as constructor so these
null string comma null string so here you

02:51.595 --> 02:55.195
can find that this this is actually
referring to this parameter as constructor

02:55.195 --> 02:59.755
which takes two string objects as input
argument and here we are passing this

02:59.755 --> 03:03.695
to null strings here so now we
are having another method that

03:03.695 --> 03:09.175
is the set age which takes this age as input
argument and which throws the below edge

03:09.175 --> 03:25.470
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

03:25.470 --> 03:29.930
so we are throwing that one so that's
why this function is throwing below age

03:29.930 --> 03:37.130
exception otherwise this age which is
not less than 18 will be assigned to the

03:37.130 --> 03:42.530
instance variable age so these dot age is
equal to age now these are display details

03:42.530 --> 03:46.730
another method we're having that is a member
function so here we are printing this

03:46.730 --> 03:52.850
name the codes the age and so on so
now let us go for our class that is the

03:52.850 --> 03:57.590
user defined exception this a main
function here we have defined two objects

03:57.590 --> 04:01.670
under the application class two objects
one is the application one another one

04:01.670 --> 04:06.350
is the application two so this is our
respective and name so we have passed this

04:06.350 --> 04:12.510
John and Java programming so this name
is nothing but this is variable is

04:12.510 --> 04:16.850
this input parameter name will get
initialized with that value whatever is we are

04:16.850 --> 04:20.750
going to pass and the course name will be
initialized with this Java programming

04:20.750 --> 04:25.310
so here this is my name and this is
the course name we're having so now

04:25.310 --> 04:29.930
application one dot display details so
the display details will be printed here

04:29.930 --> 04:35.330
so now application 2 dot set age 17
you can find that here we have passed

04:35.330 --> 04:39.890
the age 17 which is below 18
so as a result of that these

04:39.890 --> 04:45.450
applies this set age this particular method
will throw this particular method will

04:45.450 --> 04:51.270
throw below age exception so that's why
we put this line under the tri block

04:51.270 --> 05:03.565
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

05:03.565 --> 05:10.465
dot get message and e.d. print stack stress
so now let me go for the execution of

05:10.465 --> 05:17.725
the same so if I go on executing you
can find that the name of the student is

05:17.725 --> 05:22.545
John and applied for Java programming
and taking the default age 18

05:22.545 --> 05:28.045
so now in this case what is happening so the
application one dot display details it is

05:28.045 --> 05:33.345
printing accordingly so in that next
time when you are going for this set age

05:33.345 --> 05:37.905
method will be called from this application
to that is the application class

05:37.905 --> 05:44.785
object passing this input argument as
17 so as the 17 is less than 18 so it

05:44.785 --> 05:49.245
is throwing new below age exception so
automatically the catch block will get

05:49.245 --> 05:57.065
the control because this catch block is actually handling this below
edge exception e and the respective code is getting printed so e

05:57.065 --> 06:02.145
dot get message is printing
this age is under 18 and e

06:02.145 --> 06:08.965
dot print stack stress is printing the respective
outcome. I'm going to show you the outcome here

06:08.965 --> 06:16.045
So this outcome it is going to show. So you can find
that the error has occurred at the line number 44

06:16.045 --> 06:21.425
So you see at the line number 44 the error has
occurred and line number 24 is actually throwing

06:21.425 --> 06:27.525
this below edge. you can find that this line
number 24 is here and 44 is here so it is a the

06:27.525 --> 06:31.425
respective line numbers are getting
printed so in this way in this particular

06:31.425 --> 06:37.665
demonstration we have discussed how we can define user
defined exceptions thanks for watching this video
