00:01
In this question, we have to write a code in java and we have to build a class as it is given in the question.
00:12
So, we have to build a class in this case which is called as testexceptions .java.
00:34
So, here we have to put the following code in the main method.
00:43
So, it is given to us in the question and then we have to compile and run this class.
00:49
Then we have to add try or catch blocks in this.
00:54
So, we have to add try or catch blocks.
01:03
So, these blocks will be there to catch these exceptions.
01:08
So, we have to see if there is another way we can use to prevent a runtime error.
01:14
Then we have to have an account class that is there which will contain a lot of properties that is balance, owner, account number.
01:29
Then these will have two constructors.
01:32
Then everything that we have to do here is given to us in the question and we have to build a main method to do so.
01:41
So, everything is listed to us in the question.
01:44
So, let us just see how we will provide to write a code in java for the same and i will just tell you here.
01:54
So, first we have to define a public class that is testexceptions as it is as it was mentioned to us in the question.
02:04
Then we have to give a main static void and we have to give the further initializations.
02:16
So, i have done those here.
02:19
So, here as we can see that if we compile and run this class, we will encounter two exceptions that is arithmetic exception and array out of bounds exception.
02:31
As you can see, i have mentioned them over here.
02:34
So, the first exception occurs when dividing by zero that is z is equal to y divided by x as you can see in this line.
02:47
So, here we have initialized my array and then we have initialized three variables x, y and z.
02:55
We have initialized x with zero, y with 10 and with z we have given this arithmetic expression.
03:01
This first exception occurs when dividing by zero that is in this case and the second exception will occur by when we are accessing an array index that is out of bounds.
03:16
So, in this case it is my array 10 is equal to zero.
03:20
So, to catch these exceptions, the code is enclosed within a try catch block.
03:27
So, this is a try block which starts over here and it has catch over here as it was mentioned to us in the question.
03:35
So, the catch block for arithmetic exception catches the divide by zero exception and the catch block for array index out of bounds exception catches the out of bounds array access exception.
03:52
So, the exception messages are then printed to the console as you can see here by using system out print.
04:00
So, here there is another way to prevent a runtime error without trying the catch try and catch blocks.
04:10
Now, this is done by performing a proper error check and validation before executing the code that may potentially cause an error.
04:18
So, for example, we can check if the denominator is zero before performing the division operation or we can check if the array index is within the range that is the valid range before accessing it...