Java Essentials - Built in exceptions
8K views
Oct 24, 2024
Java Essentials - Built in exceptions
View Video Transcript
0:00
In this video we are going to discuss built-in exceptions
0:04
We know that exceptions in Java can be divided into two categories
0:08
One is the built-in exceptions, another one is the user-defined exceptions. So in this case, we are concentrating only on the built-in exceptions
0:17
that is some exceptions which are predefined within this Java language can be used in our coding
0:23
So some built-in exceptions are listed here. So Java has some predefined exceptions
0:29
defined exceptions and all exceptions are the subclass of the exception class
0:34
So, Thrable is a class which is having the package that is Java
0:38
. . . So, Java.Lang dot Throbble. So it has got inherited to the two subclasses
0:46
So one is the errors. So it is having the class that is the Java
0:49
. . . . . . . . . . . . . . . . . exceptions, which is having the class that is a Java. length
0:55
Under this Java. .Lang. class we are having multiple different subclasses so some of them are out of memory
1:03
error stack overflow error etc and this java langdot exception is a superclass for multiple different subclasses so i exception SQL exception file not found exception class not found exception we having this Java Lankt runtime exception which is having some subclasses here
1:26
arithmetic exception, null pointer exception, index out of bounds exception, illegal argument exception
1:33
and etc. So this is a hierarchical diagram partial we have shown here
1:40
Now, let us go for some built-in exceptions. So, arithmetic exception. From the name, it is quite obvious that when you'll be doing some arithmetic operations
1:48
and if you find some exceptions are occurring, then this exception will be thrown
1:52
So, arithmetic errors such as divide by zero. So, array index out-of-bounds exception
1:59
From the name, it is quite obvious that I'm trying to access some subscript or the location of an array
2:06
which is beyond the definition. So index exceeds the risk. range of the array. So in those cases, this array index out of bounds exception will be thrown
2:17
Null pointer exception invalid use of null reference Number format exception invalid conversion of a string to a numeric format So in those cases this number format exception may be thrown no such method exception we are going for another
2:36
type of no such method exception a requested method does not exist so in this
2:42
we are having different exceptions we are having the different descriptions so
2:46
for the better understanding let us go for one practical demonstration for the
2:50
ease of your knowledge generation here in this Java program I have defined one integer array the name of the array is my
2:59
array which is having five consecutive locations so five elements we have put
3:04
into this array and during initialization of this array we have also mentioned
3:10
the size of the array that means it is having five consecutive locations and the
3:14
subscript will be ranging from zero to four so if any subscript if we mentioned which
3:19
is beyond this range then obviously one exception will be occurring so here
3:24
here we are printing the value of this array from the subscript number 10 which is
3:29
quite well greater than the value 4 that is a maximum subscript possible for this
3:34
array and for this situation one exception will be raised and that's why we've
3:39
written a catch block just following the tri block here and this catch block is handling array index out of bounds exception and is a respective object for that and here
3:51
we are having the remedial actions that means we are printing one statement
3:54
that is invalid index of the array okay now next time we will define one string
4:01
object string class object that is a my string initialized with null and here
4:06
in this particular system dot outer print element statement we're trying to print the character at the fifth location but this my string has got
4:14
initialized with null so if I want to pick up the character from the fifth index of
4:20
this particular my string object obviously that will raise one exception so one
4:25
exception will be produced and that's why this particular code has been put within
4:30
the tri block and immediately this code is following the catch block and that is a
4:34
null pointed exception e so system dot outd printer length the string is pointing to
4:39
to a null value. So, respective string, respective message will get printed, which is the error handling part
4:47
Okay, now see, here we are trying to convert this ABC, this particular string, as an integer
4:54
So now integer.d. Parse, I.N, B, C string, which is not at all possible because
#Java (Programming Language)