WEBVTT

00:00.080 --> 00:01.040
Welcome back.

00:01.080 --> 00:02.840
We learn about dictionaries.

00:02.880 --> 00:06.240
Now we're going to learn about error handling.

00:06.480 --> 00:12.920
Sometimes your code might cause an error dividing by zero or converting bad input.

00:12.960 --> 00:19.640
Instead of crashing, use try and accept to handle errors gracefully.

00:19.760 --> 00:22.160
Let's start with this example.

00:22.200 --> 00:24.520
Try Num1 float.

00:24.520 --> 00:25.520
It's of type.

00:25.560 --> 00:26.840
Enter a number.

00:27.240 --> 00:31.280
Number two it's of type float input.

00:31.440 --> 00:33.000
Enter another number.

00:33.120 --> 00:36.640
Result Num1 over Num2.

00:36.680 --> 00:41.640
We are dividing Num1 over Num2 and print the result.

00:41.680 --> 00:51.320
Here, if I entered zero for the second number, it will give me an error because and the error that

00:51.520 --> 00:53.200
the application crashes.

00:53.200 --> 01:04.720
But since I'm using try and accept here we can throw an exception which is zero division error or value

01:04.760 --> 01:05.280
error.

01:05.440 --> 01:07.800
Please enter valid numbers.

01:08.080 --> 01:09.040
Let me run.

01:09.080 --> 01:11.880
I'll show you and clarify everything.

01:12.080 --> 01:14.680
The first number, for example 15.

01:14.720 --> 01:16.690
The second number is three.

01:16.930 --> 01:19.050
The result is 5.0.

01:19.330 --> 01:20.210
This is good.

01:20.370 --> 01:21.850
Let me run again.

01:21.890 --> 01:23.970
Enter a number 15.

01:24.210 --> 01:26.090
The second number is zero.

01:26.290 --> 01:27.290
Click enter.

01:27.570 --> 01:30.050
And can't divide by zero.

01:30.090 --> 01:37.930
This is an exception thrown by this cell because we have used the try and except.

01:37.970 --> 01:38.610
Try.

01:38.650 --> 01:40.330
We are trying the program.

01:40.370 --> 01:41.210
Any error.

01:41.250 --> 01:47.650
Go and throw those exceptions and don't allow the application to crash.

01:47.770 --> 01:49.090
Let me run again.

01:49.090 --> 01:50.210
Enter a number.

01:50.210 --> 01:51.650
For example 15.

01:51.810 --> 01:53.370
Enter another number.

01:53.530 --> 02:01.890
If the user by mistake enters a string, here we have another error.

02:02.170 --> 02:04.570
Please enter a valid number.

02:04.570 --> 02:12.370
So this exception is being thrown when the user enters a string or an invalid invalid data.

02:12.410 --> 02:13.610
Let me run again.

02:13.850 --> 02:17.530
For example 15 six G enter.

02:17.530 --> 02:20.170
Please enter valid numbers.

02:20.450 --> 02:25.370
So this is how we handle in the exceptions.

02:25.370 --> 02:32.490
How we handle errors gracefully instead of crashing using try and accept.
