1
00:00:01,580 --> 00:00:04,939
Code that is likely to fail is going to throw a checked exception.

2
00:00:07,680 --> 00:00:10,350
An exception is a failure that can crash your application.

3
00:00:14,710 --> 00:00:19,780
And there are two types of exceptions, checked exceptions and unchecked exceptions.

4
00:00:24,260 --> 00:00:27,470
In this lesson, you're going to learn to catch checked exceptions.

5
00:00:31,650 --> 00:00:34,980
An exception is a failure that the application cannot control.

6
00:00:36,530 --> 00:00:39,680
And so Jeff is going to force you to catch checked exceptions.

7
00:00:44,490 --> 00:00:47,790
I checked exception can occur from failing to connect to a website.

8
00:00:50,930 --> 00:00:52,580
Trying to access a missing file.

9
00:00:55,540 --> 00:00:58,180
Failing to access her query from a database.

10
00:00:59,850 --> 00:01:05,370
So what you can take away from this is that code is more likely to fail if it interacts with outside

11
00:01:05,370 --> 00:01:06,120
resources.

12
00:01:12,320 --> 00:01:17,300
And so a method or constructor that's prone to failure is going to throw a checked exception, if you

13
00:01:17,300 --> 00:01:20,560
want to call this method job is going to force it to handle the exception.

14
00:01:20,840 --> 00:01:26,780
In other words, job is going to force you to try to run the code and catch the exception if the code

15
00:01:26,780 --> 00:01:27,440
fails.

16
00:01:30,120 --> 00:01:31,430
Let's go through some examples.

17
00:01:33,110 --> 00:01:38,080
First things first opened up the folder, checked exceptions by following this path and the resources

18
00:01:38,300 --> 00:01:43,010
and if this path is not visible to you, then please download the updated resources from GitHub.

19
00:01:50,280 --> 00:01:56,100
All right, a common exception is the file not found exception, this failure is outside the control

20
00:01:56,100 --> 00:01:59,220
of the application because this is a checked exception.

21
00:01:59,220 --> 00:02:03,690
Job is going to force you to try and catch the exception before compiling your code.

22
00:02:07,030 --> 00:02:12,640
And so inside reading Fallsburg, Java, we're going to try, no pun intended, to read the data from

23
00:02:12,640 --> 00:02:17,790
Greetings Dot text each line inside the file as a greeting in nine different languages.

24
00:02:18,830 --> 00:02:22,910
And so back inside the class, we need to use what's known as a file input stream.

25
00:02:24,740 --> 00:02:28,130
We're going to create a new object of the file input stream class.

26
00:02:31,940 --> 00:02:37,130
And we're going to use the file input stream to open a connection to our text file greetings dot text.

27
00:02:43,110 --> 00:02:48,990
As it stands, this code is not going to compile because as I hover over the air and scroll down, we

28
00:02:48,990 --> 00:02:49,840
can see what's wrong.

29
00:02:50,370 --> 00:02:52,850
We need to handle the file, not found exception.

30
00:02:52,860 --> 00:02:54,110
It's a checked exception.

31
00:02:54,630 --> 00:02:59,370
In other words, there's a chance the user is going to enter a bad file name and there's nothing Java

32
00:02:59,370 --> 00:03:01,390
can do except throw a checked exception.

33
00:03:02,190 --> 00:03:05,750
So if you want to call this constructor, job is going to force you to handle the exception.

34
00:03:06,240 --> 00:03:09,840
In other words, it's going to force you to try to run the code.

35
00:03:16,130 --> 00:03:20,630
And if the code happens to fail, you need to catch the file not found exception.

36
00:03:23,740 --> 00:03:29,560
And if we catch an exception, we're going to print the message that it comes with print line exception

37
00:03:29,830 --> 00:03:31,240
and will get the message.

38
00:03:35,820 --> 00:03:39,210
All right, now, instead of writing the code, I'm just going to visualize the runtime.

39
00:03:42,500 --> 00:03:44,420
OK, I'll step into the constructor.

40
00:03:44,960 --> 00:03:47,780
That's not the filing straight constructor, let me step out of it.

41
00:03:48,410 --> 00:03:50,570
Step out again, OK?

42
00:03:50,570 --> 00:03:52,010
I'm going to step in once more.

43
00:03:52,010 --> 00:03:53,300
Hopefully now it works.

44
00:03:56,090 --> 00:04:01,820
And here we can see the file input stream constructor and the constructor may throw a file not found

45
00:04:01,820 --> 00:04:04,880
exception, but the file name we passed in is correct.

46
00:04:12,010 --> 00:04:13,300
So it's going to find the file.

47
00:04:18,040 --> 00:04:20,620
Everything is going to work, so there's nothing to catch.

48
00:04:21,700 --> 00:04:23,170
But if you misspell the file.

49
00:04:28,360 --> 00:04:33,850
The constructor is going to throw a file, not found exception, our code is going to catch the exception

50
00:04:33,850 --> 00:04:34,990
and print the message.

51
00:04:43,010 --> 00:04:44,500
Let's visualize what happens.

52
00:04:49,310 --> 00:04:53,780
The Phalen bitstream constructor that were passing the file name into it's going to throw an exception

53
00:04:53,780 --> 00:04:55,680
because it's not going to be able to find the file.

54
00:04:55,700 --> 00:04:56,690
Greetings, Doubler.

55
00:04:56,690 --> 00:04:57,620
Astarte text.

56
00:05:00,750 --> 00:05:02,550
The code is going to catch the exception.

57
00:05:05,050 --> 00:05:06,490
And print the error message.

58
00:05:11,620 --> 00:05:13,900
All right, let's make sure we pass in the correct filename.

59
00:05:21,220 --> 00:05:24,820
And after connecting to the file, you can read its data using scanner.

60
00:05:30,960 --> 00:05:36,150
Now, usually, Scanner receives input from the system using System NT, but this time it's going to

61
00:05:36,150 --> 00:05:38,100
receive input from the file input stream.

62
00:05:39,340 --> 00:05:40,600
And what's the issue here?

63
00:05:42,890 --> 00:05:43,520
Oh, I see.

64
00:05:51,230 --> 00:05:55,640
OK, I'm going to use a while loop that keeps running until scanner reads every single line from the

65
00:05:55,650 --> 00:06:00,530
greetings file, the while loop is going to keep running as long as the file has a next line to read.

66
00:06:03,500 --> 00:06:05,090
It should run nine times.

67
00:06:07,010 --> 00:06:11,270
During each run, print the value that scanner reads in the next line.

68
00:06:16,210 --> 00:06:17,890
OK, one by one scandal.

69
00:06:17,920 --> 00:06:23,800
Next one is going to read every single line inside greetings text and after it reads all nine lines,

70
00:06:23,800 --> 00:06:25,030
the while loop is going to break.

71
00:06:25,810 --> 00:06:27,670
So let's run the code and see what happens.

72
00:06:35,570 --> 00:06:38,870
And it prints every line from the greetings file Perfect's.

73
00:06:43,120 --> 00:06:45,430
I'm going to place breakpoints to visualize the runtime.

74
00:06:55,550 --> 00:07:00,500
Input stream, connect to the file and now scanner is going to receive all of its input from the file

75
00:07:00,500 --> 00:07:06,230
input stream in line by line scan, the next line is going to read all the input from the readings file.

76
00:07:10,270 --> 00:07:16,240
And one scanner reads every line once there isn't a next line anymore, the wire loop is going to break

77
00:07:16,240 --> 00:07:17,320
and it ends.

78
00:07:21,490 --> 00:07:23,770
All right, I'm going to go back and misspell the file.

79
00:07:30,110 --> 00:07:35,570
This line of code fails and throws an exception, our code catches the exception and prints an error

80
00:07:35,570 --> 00:07:36,140
message.

81
00:07:38,380 --> 00:07:42,730
OK, before we wrap up with this class, don't forget to close the scanner object.

82
00:07:51,130 --> 00:07:53,620
All right, now open reading files to Java.

83
00:07:59,970 --> 00:08:05,120
This code is the same as before, but this time the low data function contains code that throws an exception,

84
00:08:05,490 --> 00:08:06,570
the same code as before.

85
00:08:07,560 --> 00:08:12,240
Here we can do two things we can use to try catch block inside the function.

86
00:08:13,440 --> 00:08:19,290
Or we can declare that the code inside this function could fail and throw a file not found exception.

87
00:08:30,690 --> 00:08:33,330
Woops, I'll have to autocomplete it get.

88
00:08:38,500 --> 00:08:44,650
And now, because low data throws an exception, Java forces, whoever is calling it, to try to call

89
00:08:44,650 --> 00:08:45,310
this function.

90
00:08:57,330 --> 00:09:02,370
And if the function that we're calling happens to throw an exception, if it happens to fail, you need

91
00:09:02,370 --> 00:09:04,380
to catch the file not found exception.

92
00:09:06,280 --> 00:09:09,320
And after catching the exception, print the message that it comes.

93
00:09:17,890 --> 00:09:21,510
All right, this should function the same way I run it.

94
00:09:40,680 --> 00:09:41,670
And beautiful.

95
00:09:46,330 --> 00:09:50,890
Here's another example of a checked exception, the malformed you are the exception.

96
00:09:51,220 --> 00:09:54,070
It's a failure that once again the application can't control.

97
00:09:54,070 --> 00:09:58,480
So job is going to force you to try and catch the exception before compiling your code.

98
00:10:01,690 --> 00:10:05,680
So inside Pursey World Java, we're going to try to pass the Google dot com your.

99
00:10:07,400 --> 00:10:10,350
To create a new object of the moral class.

100
00:10:22,850 --> 00:10:26,180
HTTPS colon double slash.

101
00:10:27,970 --> 00:10:30,270
Without Google dotcom.

102
00:10:31,510 --> 00:10:36,460
As it stands, this code is not going to compile once again, hover over there to see what's wrong.

103
00:10:37,590 --> 00:10:43,710
The euro constructor throws a malformed Eurail exception, so if you want to call this instructor,

104
00:10:43,890 --> 00:10:46,000
JAV is going to force you to handle the exception.

105
00:10:46,440 --> 00:10:49,380
In other words, it's going to force you to try to run the code.

106
00:10:55,050 --> 00:10:59,810
And if the code happens to fail, you need to catch the malformed, you are an exception.

107
00:11:09,080 --> 00:11:12,350
And after catching the exception, print the message that it comes with.

108
00:11:18,380 --> 00:11:20,120
All right, if you're on this code.

109
00:11:24,400 --> 00:11:26,770
It should work because DRL is well formed.

110
00:11:28,270 --> 00:11:29,460
And indeed, it does.

111
00:11:31,150 --> 00:11:36,550
Now, as a side note, the euro is composed of a protocol, a host and a path.

112
00:11:38,730 --> 00:11:45,660
So from the Yooralla object, we can call some methods defined in the early class, so I'll print the

113
00:11:45,660 --> 00:11:46,380
protocol.

114
00:11:52,910 --> 00:11:54,560
I'll print the hosts.

115
00:11:56,140 --> 00:11:57,340
You know, get the path.

116
00:12:02,990 --> 00:12:07,820
All right, now I'm going to add a path of images so that we can get an interesting output.

117
00:12:10,100 --> 00:12:11,050
Run the code.

118
00:12:18,560 --> 00:12:21,020
In passing, the URL was successful good.

119
00:12:22,850 --> 00:12:24,890
Prince, the protocol, the host and the path.

120
00:12:31,570 --> 00:12:34,450
Now, if you input a malformed, you are a.

121
00:12:38,530 --> 00:12:44,620
This line of code is going to throw an exception, nothing after it runs instead of a code catches the

122
00:12:44,620 --> 00:12:45,340
malformed.

123
00:12:45,340 --> 00:12:48,190
You are an exception and it prints its message.

124
00:12:48,550 --> 00:12:51,580
Unknown protocol, a bunch of nonsense.

125
00:12:52,870 --> 00:12:53,290
OK.

126
00:12:56,800 --> 00:12:58,960
Now, open pass, you are allowed to dodge other.

127
00:13:03,590 --> 00:13:08,300
Once again, the code is the same as before, but this time the parts you are afunction contains code

128
00:13:08,300 --> 00:13:09,440
that throws an exception.

129
00:13:09,950 --> 00:13:15,980
Here we can do two things we can use to try catch block inside the function, or we can declare that

130
00:13:15,980 --> 00:13:18,500
the code inside this function throws a malformed.

131
00:13:18,500 --> 00:13:19,520
You are an exception.

132
00:13:25,840 --> 00:13:31,360
And because past you are out throws a checked exception job is going to force whoever is calling it

133
00:13:31,360 --> 00:13:32,740
to try to run the code.

134
00:13:38,130 --> 00:13:42,510
And if the code happens to fail, you need to catch the malformed, you are an exception.

135
00:13:49,110 --> 00:13:52,470
And after catching the exception, print the message that it comes with.

136
00:13:58,070 --> 00:13:59,000
I run the code.

137
00:14:13,670 --> 00:14:14,600
This works.

138
00:14:18,120 --> 00:14:21,150
I'll try again where they bade you all.

139
00:14:22,200 --> 00:14:27,600
And the parts you are afunction that we tried to call throws a malformed you are an exception, that

140
00:14:27,600 --> 00:14:31,260
exception gets cut and it's because the protocol doesn't exist.

141
00:14:36,210 --> 00:14:38,820
In this lesson, you learn to catch checked exceptions.

142
00:14:39,210 --> 00:14:41,830
An exception is a failure that can crash your application.

143
00:14:42,240 --> 00:14:43,860
There are two types of exceptions.

144
00:14:44,340 --> 00:14:47,570
There are checked exceptions and unchecked exceptions.

145
00:14:48,420 --> 00:14:51,540
I checked exception is a failure that the application cannot control.

146
00:14:51,960 --> 00:14:55,980
A method or constructor that is prone to failure is going to throw a checked exception.

147
00:14:56,790 --> 00:15:02,070
And if you want to call this method, job is going to force you to try to run the code and catch the

148
00:15:02,070 --> 00:15:04,050
exception if the code fails.

