Throw exception object for exception handling – Basic Java Fast

Throw exception object for exception handling – Basic Java Fast


Hello everybody, welcome to the Java
course. When we learn exception handling, it is usually the case that the java
virtual machine generates an Exception object, and then we catch it in our
exception handling code. Actually, can we really create our own Exception objects
by ourselves? Yes, we can. We can really throw an Exception object by ourselves.
So what does it mean by that? I’m going to tell you this idea in this video. So
suppose I’m going to do simple division So I need to create a Scanner object. In
doing so, I need to import the Scanner class first Okay. And then I can make use
of the Scanner object created here to give me two integers. And then I will do
the division of these two integers The two integers are “a” and “b” Okay, I will give a prompt first and then I will
let the user input a certain integer to me. And the dividend will be stored in
variable “a”. I can do the same thing for my divisor, which will be stored in
variable “b” And then in general we can do the division directly by dividing “a” by “b” and then I can say that the program is finished. And I can also close the
Scanner object at the end. So for this division, it is possible that the divisor
is 0. What will be the case when “b” is a actually zero according to the user input?
Let us see. Save it, run it The dividend can be 4, which is an integer. And how
about the divisor? If I put 0, I will get the exception called arithmetic
exception. The reason is that I divide one value by 0. And the exception occurs
at line number 13 shown here, ok. So we can see that the Exception object is
given out by the Java Virtual Machine, ok So if we want to create our own
Exception object like the Java Virtual Machine
does for us, we can do something like this We can really throw our own
Exception object So I will give you an example of the creation of our own
exception object How can we do that? We can do something like this If the
divisor is equal to zero, I will simply throw an Exception object
whose data type is ArithmeticException, which is a subclass of the general
Exception class. And we have to use the “new” keyword here because we really
want to create ArithmeticException object. And the keyword to help me give
me an object is the word “throw” here, okay So if the divisor is equal to zero, I
will really create my own ArithmeticException object. And of
course we have to use the “new” keyword to really create an object in
the Java Virtual Machine Okay. If the divisor is zero, I will
really throw the object of ArithmeticException datatype.
If the divisor is not equal to zero, the throwing action will not be executed. Instead,
I’m going to do the printing which is performed on the next line of code,
okay So if the ArithmeticException object is really created, I need to
handle it by catching it first Now I’m going to catch an object of ArithmeticException
data type. And the reference variable for that object is simply “ex”.
So if the try part really gives me an ArithmeticException object, the
catch part followed by that try part is able to handle it I will simply say that divisor fails
to help me do the division correctly Of course I can also do the finally part In the finally block,
I simply say that you entered a valid input or your program is able to catch
the exception correctly. After that, I’m going to close the Scanner object that I
used to extract the numbers coming from the user input. So in this case, we can
see the effect by running the program again. We have to take note of the try,
catch, and finally parts, especially the case when we throw our new ArithmeticException object Save it, run it Suppose my dividend is 6. And my divisor is simply
2. In this case, I’m able to print out the quotient which is simply equal to 3, ok.
And I can also do the finally block by printing the whole statement out and
by closing the Scanner object that I created previously. So this is the
case the ArithmeticException object is not really created. But how about a
situation when the divisor is equal to 0? We can see the case now by running it
again I put seven as the dividend, 0 as divisor.
Okay, when the divisor is equal to zero the ArithmeticException object is
really created by using the new keyword And the try block is going to throw that
object to the catch block. And the catch block is able to handle the ArithmeticException object, okay. And the object once caught by the catch block, the catch
block is able to print out the statements from inside the catch block.
It simply tells me that your divisor failed, okay. So in this case, the
ArithmeticException object can be handled by the catch block. And after the
handling by the catch block, I’m able to do the finally block. So I’m going
to continue my program gracefully by printing out the long statement here. And
I can also close the Scanner object created previously here, okay. So we can
see that we can really create our own Exception objects by using the “throw”
keyword together with the “new” keyword And after we have created our own
Exception object, we’d have to catch it in the catch block next to the
corresponding try block. This is the end of the video. If you have any questions
about my video, feel free to leave your comments on the comment section below
the video. If you like this video, please give me a like and please subscribe to
my channel. Thank you for watching

Leave a Reply

Your email address will not be published. Required fields are marked *