Java Essentials - User defined exceptions
14K views
Oct 24, 2024
Java Essentials - User-defined exceptions
View Video Transcript
0:00
In this video we are discussing user defined exceptions
0:05
In the previous video we have discussed built-in exceptions. That means those exceptions were predefined and pre-retained within the Java program
0:15
So now in case of user-defined exceptions, we can create our custom exceptions
0:20
We can write one class and the class should inherit or the class should extend the exception class
0:26
And that is the common practice while defining user-defaults. defining user defined exceptions
0:33
Programmer can create custom exceptions and they are known as the user defined exceptions
0:40
To create an exception, we need to define a class for the exception and extends it to the
0:47
exception class. As I have mentioned earlier, that if I define one class, the name of the class may be my
0:53
exception, but it is of a common practice to make the class extending exception class
0:58
so that I can define my custom exception. When we need to generate the newly created exception
1:06
we will use the throws keyword for the method and it indicates that the function can throw an exception
1:15
So in our code we'll be using throws keyword which will indicate that this respective function
1:22
can throw some exception in our code. So this is the concept
1:27
So for the better, clarity on this concept let us go for one practical demonstration where we'll be
1:33
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
1:47
is below age exception which extends the exception class here and this is a
1:52
constructor and this a constructor of the class below age exception and from this
1:56
constructor it is calling the best class constructor here the best class is
2:00
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
2:11
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
2:19
the name of the constructor which is taking this name and the course as input
2:23
argument so this name and course the instance variables are getting initialized
2:28
here age is also getting initialized with the default value of 18 this is
2:33
non parameter as constructor obviously the name of the constructor will be same as
2:38
that of the class name so the name of the constructor is application which is
2:42
non parameterized and from this non parameter as constructed we are calling
2:46
the parameter as constructor so these null string comma null string so here you
2:51
can find that this this is actually referring to this parameter as constructor
2:55
which takes two string objects as input argument and here we are passing this
2:59
to null strings here so now we are having another method that
3:03
is the set age which takes this age as input argument and which throws the below edge
3:09
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
3:25
so we are throwing that one so that's why this function is throwing below age
3:29
exception otherwise this age which is not less than 18 will be assigned to the
3:37
instance variable age so these dot age is equal to age now these are display details
3:42
another method we're having that is a member function so here we are printing this
3:46
name the codes the age and so on so now let us go for our class that is the
3:52
user defined exception this a main function here we have defined two objects
3:57
under the application class two objects one is the application one another one
4:01
is the application two so this is our respective and name so we have passed this
4:06
John and Java programming so this name is nothing but this is variable is
4:12
this input parameter name will get initialized with that value whatever is we are
4:16
going to pass and the course name will be initialized with this Java programming
4:20
so here this is my name and this is the course name we're having so now
4:25
application one dot display details so the display details will be printed here
4:29
so now application 2 dot set age 17 you can find that here we have passed
4:35
the age 17 which is below 18 so as a result of that these
4:39
applies this set age this particular method will throw this particular method will
4:45
throw below age exception so that's why we put this line under the tri block
4:51
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
5:03
dot get message and e.d. print stack stress so now let me go for the execution of
5:10
the same so if I go on executing you can find that the name of the student is
5:17
John and applied for Java programming and taking the default age 18
5:22
so now in this case what is happening so the application one dot display details it is
5:28
printing accordingly so in that next time when you are going for this set age
5:33
method will be called from this application to that is the application class
5:37
object passing this input argument as 17 so as the 17 is less than 18 so it
5:44
is throwing new below age exception so automatically the catch block will get
5:49
the control because this catch block is actually handling this below edge exception e and the respective code is getting printed so e
5:57
dot get message is printing this age is under 18 and e
6:02
dot print stack stress is printing the respective outcome. I'm going to show you the outcome here
6:08
So this outcome it is going to show. So you can find that the error has occurred at the line number 44
6:16
So you see at the line number 44 the error has occurred and line number 24 is actually throwing
6:21
this below edge. you can find that this line number 24 is here and 44 is here so it is a the
6:27
respective line numbers are getting printed so in this way in this particular
6:31
demonstration we have discussed how we can define user defined exceptions thanks for watching this video
#Java (Programming Language)