1
00:00:01,160 --> 00:00:03,650
Unchecked exceptions happened during the runtime.

2
00:00:06,000 --> 00:00:12,840
There are two types of exceptions, checked exceptions and unchecked exceptions in the previous lesson,

3
00:00:12,840 --> 00:00:18,030
you learned to catch checked exceptions, but in this lesson you're going to learn to fix unchecked

4
00:00:18,030 --> 00:00:18,930
exceptions.

5
00:00:24,120 --> 00:00:27,750
Both checked and unchecked exceptions are failures.

6
00:00:29,020 --> 00:00:33,700
I checked exception is a failure that's outside the control of the application, so Java is going to

7
00:00:33,700 --> 00:00:37,690
force you to try to run the code and catch the exception if it fails.

8
00:00:39,890 --> 00:00:46,490
An unchecked exception is the result of badly written code, never, ever catch an unchecked exception,

9
00:00:46,640 --> 00:00:50,480
catching an unchecked exception is the same as sweeping the problem under the rug.

10
00:00:50,660 --> 00:00:53,540
If your code is badly written, don't cover the problem.

11
00:00:53,720 --> 00:00:56,570
Instead, fix your code so that it doesn't fail.

12
00:00:59,990 --> 00:01:02,850
Unchecked exceptions are runtime exceptions.

13
00:01:02,870 --> 00:01:07,340
In other words, the failure happens while the app is running and will crash your application.

14
00:01:08,180 --> 00:01:10,880
The most common examples are right index out of bounds.

15
00:01:10,880 --> 00:01:13,220
Exception, null pointer exception.

16
00:01:13,610 --> 00:01:14,720
A legal argument.

17
00:01:14,720 --> 00:01:17,210
Exception in mismatch exception.

18
00:01:17,510 --> 00:01:19,260
An illegal state exception.

19
00:01:19,760 --> 00:01:20,800
There are so many more.

20
00:01:21,380 --> 00:01:22,550
Let's have a look at each one.

21
00:01:26,910 --> 00:01:32,730
Go to this path in your resources and open unchecked exceptions, and if you're missing this path for

22
00:01:32,730 --> 00:01:36,450
whatever reason, make sure to download the updated resources from GitHub.

23
00:01:45,190 --> 00:01:50,040
The array index out of bounds exception is thrown if you index outside the bounds of the array.

24
00:01:52,680 --> 00:01:57,870
So inside runtime, except you one Java, we're going to try to index the fifth element from an array

25
00:01:57,870 --> 00:01:59,670
that stores only three elements.

26
00:02:12,450 --> 00:02:17,370
And it crashes the application, throws an array index out of bounds exception.

27
00:02:18,760 --> 00:02:20,120
Now, there are two things we can do.

28
00:02:20,170 --> 00:02:21,730
You can try to run this code.

29
00:02:31,140 --> 00:02:33,090
And catch the exception if it fails.

30
00:02:34,570 --> 00:02:39,790
We're going to catch the array index out of bounds exception and print the message that it comes with.

31
00:02:52,550 --> 00:02:54,590
This time, the application doesn't crash.

32
00:02:55,010 --> 00:03:00,080
The code fails and throws an exception, but we're catching the exception and printing its message.

33
00:03:00,590 --> 00:03:05,880
And that being said, never, ever do that, never catch an unchecked exception.

34
00:03:06,470 --> 00:03:11,200
Remember that catching and unchecked exception is the same as sweeping the problem under the rug.

35
00:03:12,170 --> 00:03:17,540
When Jova throws an unchecked exception, like a right index out of bounds, that means there's something

36
00:03:17,540 --> 00:03:19,750
wrong or there's something missing in your code.

37
00:03:20,180 --> 00:03:24,140
So instead of catching the failure thickset so that it doesn't happen.

38
00:03:27,750 --> 00:03:28,550
And there you go.

39
00:03:32,250 --> 00:03:37,200
Now, the null pointer exception is thrown if you try to access something, if you try to dots and tags

40
00:03:37,200 --> 00:03:37,760
from a null.

41
00:03:40,320 --> 00:03:45,570
So inside one exception to that job, I try to call a method from the string class, I'm going to call

42
00:03:45,570 --> 00:03:47,490
a random method like two lowercase.

43
00:04:05,300 --> 00:04:11,060
And the code in line for fails and throws a null pointer exception, crashing the application, the

44
00:04:11,060 --> 00:04:16,399
null pointer exception is an unchecked exception because it happens during the runtime and when you

45
00:04:16,399 --> 00:04:19,519
get an unchecked exception, that means there's something wrong with your code.

46
00:04:20,269 --> 00:04:20,930
Let's fix it.

47
00:04:23,310 --> 00:04:25,830
So here I'm going to say if word is equal to null.

48
00:04:29,650 --> 00:04:31,450
Then I'll print the word is no.

49
00:04:38,000 --> 00:04:42,020
Otherwise, and only then are we allowed to call were lowercase.

50
00:04:50,990 --> 00:04:54,780
So as you can see, the unchecked exception forced me to improve my code.

51
00:04:54,800 --> 00:04:56,450
This was fully within my control.

52
00:04:57,260 --> 00:05:00,600
And the reason this is being highlighted is because it's never going to run.

53
00:05:00,670 --> 00:05:01,710
Word is always no.

54
00:05:01,730 --> 00:05:04,700
So it's that code anyway is not too important.

55
00:05:05,540 --> 00:05:06,720
But here's a common mistake.

56
00:05:06,980 --> 00:05:11,570
Normally, you should always use the equals method to compare a string, but you can't use it to check

57
00:05:11,570 --> 00:05:16,520
for a nail because dots and doxxing from a null trying to access something from a null is always going

58
00:05:16,520 --> 00:05:17,900
to throw a nail pointer exception.

59
00:05:28,770 --> 00:05:29,990
All right, let's fix it.

60
00:05:35,390 --> 00:05:37,790
Never replace if with try catch.

61
00:05:38,090 --> 00:05:44,150
I have no idea why people do this, if else statements are a lot faster than try catch and they're easier

62
00:05:44,150 --> 00:05:44,810
to work with.

63
00:05:46,390 --> 00:05:51,430
Besides only ever use, try and catch to catch the exceptions.

64
00:05:56,930 --> 00:06:01,610
Now, the input mismatch exceptions thrown by scanner, if the user enters a value that it wasn't expecting.

65
00:06:04,000 --> 00:06:06,700
So run the code inside runtime, except in three Java.

66
00:06:18,050 --> 00:06:21,050
Skinner is expecting an integer, but we're going to pass in a string.

67
00:06:23,220 --> 00:06:29,310
And we get an input mismatch exception, the input mismatch exception is an unchecked exception because

68
00:06:29,310 --> 00:06:30,900
it was thrown during the runtime.

69
00:06:31,500 --> 00:06:33,040
When you get an unchecked exception.

70
00:06:33,090 --> 00:06:36,000
Then there's something wrong or there's something missing in your code.

71
00:06:36,920 --> 00:06:41,960
In this case, what's missing is code that checks in advance if the next is an integer.

72
00:06:43,340 --> 00:06:47,270
And only then are we going to pick up the values, scandal and extent.

73
00:06:50,390 --> 00:06:54,770
Otherwise, we're going to have to pick it up using Scandi next line, because next line can pick up

74
00:06:54,770 --> 00:06:55,160
anything.

75
00:07:00,190 --> 00:07:01,480
And also, we'll tell the user.

76
00:07:02,860 --> 00:07:03,670
Not a no.

77
00:07:07,950 --> 00:07:09,150
OK, we on the code.

78
00:07:12,580 --> 00:07:18,010
I'll put in a word, and just like that, we handled the unchecked exception by fixing our code.

79
00:07:18,820 --> 00:07:23,590
Now we can say the code is reliable because there's no way that it's going to crash no matter what I

80
00:07:23,590 --> 00:07:24,390
throw at it.

81
00:07:26,090 --> 00:07:30,230
All right, we can actually take this a step further, we can use a while loop that runs forever.

82
00:07:44,710 --> 00:07:48,490
If the next input is an integer, then we'll print it and break the loop.

83
00:07:50,690 --> 00:07:54,320
And if the next input is anything else, then we'll pick it up with next line.

84
00:08:02,730 --> 00:08:06,930
And now the code is going to keep asking the user to enter a number until they do.

85
00:08:12,590 --> 00:08:17,930
If you can take anything away from this lesson, take this rule of thumb, catch a exception because

86
00:08:17,930 --> 00:08:22,490
it's outside the applications control, in any case, Judge is going to force you to catch it before

87
00:08:22,490 --> 00:08:27,800
compiling thickset, unchecked exception because it implies that there's something wrong or there's

88
00:08:27,800 --> 00:08:29,240
something missing in your code.

89
00:08:33,100 --> 00:08:38,200
In this lesson, you fixed three unchecked exceptions, an unchecked exception is a one time exception.

90
00:08:38,530 --> 00:08:44,340
The failure happens during the runtime because of poorly written code and never catch an unchecked exception.

91
00:08:44,800 --> 00:08:49,090
Instead, fix the code or improve the code so that the failure doesn't happen.

