Java Essentials - Exception handling in java
13K views
Oct 24, 2024
Java Essentials - Exception handling in java
View Video Transcript
0:00
In this video we are discussing exception handling in Java
0:05
Exception means erroneous condition. Let us suppose I've defined one array having got size of 10, but I want to keep a data
0:13
at the 20th location. Then exception will take place. I want to divide two numbers A by B, but B is having the value 0
0:22
So that is a source of producing or getting exceptions. Let us suppose I have defined one integer array, but I'm going to be a number
0:29
going to keep one string data onto that integer array. So that will produce one exception
0:36
So in this way, there are so many different ways or cases where we can have the exceptions
0:42
Let us consider another case. We are trying to do a data insertion on a table where the table is not existing
0:49
So you see, we are trying to read something from the data file, but the file is not present
0:54
in the respective folder or the path is wrong. all these cases the exceptions will be there and whenever such exceptions will
1:03
occur during the execution of a Java program then obviously the program will
1:08
have will get halted will get terminated at the midpoint so how to handle
1:14
this exception and that's why the origination of this chapter there is an
1:18
exception handling in Java an exception or exceptional event is a problem that
1:26
arises during the execution of a program that means during the runtime of a program
1:31
when an exception occurs the normal flow of the program will be disrupted and
1:36
the program or the application terminates abnormally a very popular exception is
1:44
dividing some number by another variable but the variable is containing the value
1:49
zero or dividing a number by the zero directly in those cases exceptions will be
1:54
generated so just consider this particular diagram here so here we're having the
2:00
class object from the object class this thruble class has got inherited from the
2:05
throughable we're having this errors class and these exceptions and from these
2:09
exceptions this runtime exceptions and other exceptions will be inherited so in this
2:14
way we're getting the hierarchical diagram hierarchical class diagram regarding this exception handling in Java so how to handle exceptions
2:24
A method catches an exception using a combination of the try and the catch keywords
2:32
So, try block. So after the writing the try keyword, we'll be putting the curly browsers and then we'll
2:37
be putting some set of statements there where we are expecting some exceptions may take place
2:43
So if those statement execution may cause some exceptions during the execution, then those statements
2:51
must be kept under the tri-block and catch-block should follow the try-block immediately
2:57
A try-catch block is placed around the code that might generate an exception
3:03
We can enclose the instruction like this respective syntax. So try, then code that can generate exceptions, we are guessing, and then after completion
3:13
of this tri-block, that is closing of this corlybress, immediately we will be having the catch
3:18
block so catch exception name e then within the cul-libres will be writing the respective exception
3:25
handling statements that means when the exception will occur then what we are going to do in this way we can have multiple catch blocks in any order which will be following there and these catch blocks can handle individually each and every exception separately
3:41
to provide the remedial actions to be taken when the exception will occur during the runtime of a Java program
3:49
Another block is used after the try or try catch block that is known as the finally
3:54
So codes inside the finale block always gets executed irrespective of the fact whether any exceptions
4:00
has occurred or has not so this is the concept of our exception handling so for
4:06
the better conception for the better understanding let us go for one practical
4:10
demonstration using Java code and we are going to use that one we're going to
4:15
give the demonstration right now after this in this Java program demonstration
4:21
we are discussing exception handling exception means erroneous condition so within the tri-block we will be writing some set of statements where you are expecting
4:30
some error may take place. So here we are doing one calculation that is I and T result
4:36
is equal to 22 by 0. We know that division by 0 is illegal. So as a result of that, when
4:42
this division by 0 will try to take place, then in that case this line will remain unreachable
4:48
unexecuted, the control will try directly will come to the respective catch block which
4:53
is just following the tri-block where the arithmetic exception has been caught. So how to write
4:59
the catch block within the catch block we'll be writing this the exception name and the exception
5:04
object there is a catch the exception and handle it in a correct way we're supposed to write the
5:10
respective remedial actions to be taken when the exception will take place and then catch block
5:15
will be followed by the final block obviously that is optional it is not mandatory that always the
5:20
catch blocks will be followed by the final block in the final block we'll be writing some statements
5:25
we can put some block of course there what is the purpose of the
5:29
the final block whenever exception will occur in the tri-block then also the final block
5:34
will get executed after execution of the respective catch block if that if no error has occurred
5:40
no exception has occurred in the tri-block then also the final block will get executed
5:46
that means the finally block will get executed irrespective of the fact whether exception has
5:50
occurred in the tri-block or not and then end of the program the rest part of the code will get
5:56
executed so now let me go for the execution at first you can find that dividing dividing by zero is not allowed so that means this
6:04
particular line has got the control that means system dot out of print
6:08
and result is equal to plus results so that line has remained unreachable and
6:12
then the exception is Java dot land dot arithmetic exception and what is the
6:17
error message that is our division by zero so that is the error message and then
6:23
operation performed so it is coming from this finery block and and end of the program it is coming from this rest of the line after the try catch finally block so in this way you have got the idea so now let me make it too so that no exception will take place then result is the golden event that means this line has got the control the catch block it hasn't got the control the control has come directly to the final block operation performed that message has got printed and end up the program that message also got printed here so this is the purpose of our
6:59
try catch and finally so let us extend this program to some more extent so we'll be
7:05
going for the keyboard inputs so we be going for data 1 by data 2 so data 1 and data two will be taken from the keyboard as input so here you are defining some set up
7:36
so there is a input stream reader so we shall go for and just this one and it is
7:44
asking that it is going for the import so it is under Java.io package so input
7:49
stream reader has got imported next one you shall go for buffer reader
7:55
buffer reader beer is equal to new buffer reader
8:05
isr this is a buffer reader so import has to be done Java.io is a package
8:14
name then from this buffer reader beer we shall go for I&T data 1 is equal to
8:22
br.d. So I shall go for this readline and it returns string you can easily find
8:28
that return string so that's why I must be doing a typecasting here so
8:32
this a red line is there and it is taking that is a change of type data 1 to string otherwise we can go for integer
8:42
dot parsa nt integer dot parsa nt this particular string has to be
8:50
converted to integer and then again one red underline it is indicating that
8:56
add throw a declaration or surround with the try catch so surround with the
9:02
tri-catch you can find that this particular this particular parse i.n t deadline it has got surrounded with this number format
9:09
exception and i.o exception so it has got surrounded with that otherwise you can
9:15
write in this way I press control z surround with the try multi-catch so in this
9:21
way you can also go for this multi-catch that means number format exception or I
9:26
you exception then this respective code will get executed otherwise we can go for
9:30
another way other is you can write it it in this way so that is also very much useful also
9:39
we can segregate them because we may have different remedial actions to be taken
10:05
and there is the final block we are having so we are having this
10:13
arithmetic exception we're having this number format exception and then I exception there so let me go for this another data has to be read so let it be
10:27
data 2 and you are performing data 1 by data 2 we are performing data 1 by data 2 so now we are having this set of multiple catch
10:40
blocks are there so let me go for the execution all the required inputs have been done by this eclipse
10:52
Now let us go for the execution. We are just executing the same
10:56
So we are executing and then here we are writing the first value, say 100
11:02
The second value is a 20. I'm getting the result 5 and operation performed end of the program
11:08
So let us go for one prompt here for the better understanding. running
11:41
100 second number we are putting as 20 result is equal to 5 is coming if I execute the same if I provide the first number as 100 second number as 0
11:51
Division by 0 is not allowed that means the arithmetic exception has been caught and the operation performed and end of the program
11:58
Now next time if I put the first number as say 100 the second number has said 10 then number format exception has taken place operation performed and end up the program is getting executed
12:11
that is whenever we are providing providing 10 TEN here so that cannot be
12:16
converted to integer so whenever you are providing 10 then 10 cannot be
12:21
converted to integer because we have written 10 not in one zero format but in
12:26
TEN that is in the form of string so in that case in that case this
12:30
respective number format exception has been caught here so I want to show you
12:35
one thing that instead of writing this particular I'm just making them comment
12:41
there's a number format exception and this I'm going for this up to this so now
12:52
see this arithmetic exception and number format exception I put them under comment
12:56
and then after this catch block we are providing exception which is the
13:05
superclass of all the exceptions so this exception and this exception has been caught
13:11
has taken place so now what will happen when about this arithmetic exception or
13:16
number format exception will take place then exception will handle the situation let
13:21
me go for the execution so 100 and 0 you can find that exception has taken place
13:26
so now again we are executing this 110 110 you can find that exception has
13:35
taken place that means instead of having this arithmetic exception catch block
13:40
and then number format exception catch block we can have a single exception catch block that is a
13:46
catch exception e exception has taken place the remedial actions are to be written in here and finally
13:53
is there always will get executed even if exception has occurred or has not occurred so this particular
14:00
example i think has cleared your doubts that what is exception and how to write the exception
14:05
handling program in our java code so let me put back my code
14:10
in this case. So what about the rest of the exceptions, which I haven't caught here
14:17
will be caught by this exception catch block. Thanks for watching this video
#Java (Programming Language)