1

00:00:01,170  -->  00:00:07,090
Let's begin by first learning about how exceptions are represented on how they can be hundred.

2

00:00:07,650  -->  00:00:13,640
An exception is simply an object of the class through habit or one off it's up process.

3

00:00:13,800  -->  00:00:21,870
So to simply an object like any other good job object on Trouble is from the outline package under the

4

00:00:21,870  -->  00:00:25,320
superclass of all exceptionally literate classes.

5

00:00:25,500  -->  00:00:30,950
And as I mentioned earlier an exception indicates an exceptional situation like a partner solver going

6

00:00:30,950  -->  00:00:36,720
on in such a case a method or a constructor gender's the exception.

7

00:00:36,720  -->  00:00:43,300
Now let's head over to Eclipse and we will learn about exceptions by directory writing some code.

8

00:00:44,220  -->  00:00:47,280
I've created a new package called com not semantics.

9

00:00:47,280  -->  00:00:53,360
There are exceptions within the demo project and within the exceptions package classes have been created

10

00:00:53,370  -->  00:00:58,830
one has exceptions them all and other is hitcher to be connected and within the exceptions class there

11

00:00:58,830  -->  00:00:59,460
are two amateur's.

12

00:00:59,460  -->  00:01:05,670
One is mine and the other is share on the share that is very similar with a shared method that we have

13

00:01:05,670  -->  00:01:07,870
seen in the in the earlier project.

14

00:01:08,100  -->  00:01:13,890
So if you recall in Threepio project the sheer method was used to share a bookmark with a fictitious

15

00:01:13,890  -->  00:01:15,920
partner Web site called good snips.

16

00:01:16,030  -->  00:01:20,620
And so the shared method is also very similar to that and within the history to be connected with us

17

00:01:20,630  -->  00:01:20,770
.

18

00:01:20,790  -->  00:01:26,940
We have this method called Center and the main method actually invokes shared motard and shared method

19

00:01:27,030  -->  00:01:27,880
in done in Wilkes.

20

00:01:27,930  -->  00:01:34,200
The sentiment that it's a static method and has GTP connect Ansen method actually does that are real

21

00:01:34,200  -->  00:01:36,930
sharing of data of it with snips.

22

00:01:37,080  -->  00:01:40,310
OK so even though share is supposed to do the sharing.

23

00:01:40,350  -->  00:01:46,030
But the actual sharing will be done by send and it has all the low level details for shading off the

24

00:01:46,040  -->  00:01:48,220
end of the with the partner Web site.

25

00:01:48,410  -->  00:01:48,700
OK.

26

00:01:48,720  -->  00:01:50,070
So that's the assignment.

27

00:01:50,250  -->  00:01:53,440
So we have just three template metrics in each method.

28

00:01:53,460  -->  00:01:58,850
We have to print statements one or the top one at the end at the top it says in site for example here

29

00:01:58,850  -->  00:02:02,410
it says inside me are not the end users end of Main.

30

00:02:02,430  -->  00:02:09,270
Similarly in shared how insights share on and off each year on scene with the Send method also and these

31

00:02:09,270  -->  00:02:13,700
print statements have been included in order to look at the control flow.

32

00:02:13,980  -->  00:02:19,640
So the control flow defers based on whether or not an exception is being has been generated.

33

00:02:19,950  -->  00:02:23,920
If an exception is generated then you would how one type of water flow.

34

00:02:24,180  -->  00:02:28,140
But if the next option is non-gendered then you will see a different control flow.

35

00:02:28,140  -->  00:02:31,860
So in order to keep track of dock we have these print statements.

36

00:02:32,140  -->  00:02:36,290
And so let's just go ahead and start filling up these methods.

37

00:02:36,330  -->  00:02:41,860
Let's begin with the sand method and it has three parameters one in parameter on string parameters.

38

00:02:42,120  -->  00:02:47,130
So here the data is one of the parameters and the goal of this method is to send this date talked with

39

00:02:47,130  -->  00:02:53,160
a partner up site and destination retailers how the it is going to be transmitted.

40

00:02:53,190  -->  00:02:56,540
So we have two ways in which the data can be shared.

41

00:02:56,550  -->  00:03:03,670
So the first way if destination is zero it means that there will be written to a file on the local Sarber

42

00:03:04,140  -->  00:03:08,940
on the harness and then there'll be some kind of a script which will push their data to the partner

43

00:03:08,940  -->  00:03:09,560
Web site.

44

00:03:09,570  -->  00:03:10,590
So that's one way.

45

00:03:10,800  -->  00:03:17,220
And if destination is one it means that the Send method will programmatically push their data directly

46

00:03:17,220  -->  00:03:22,660
to the partners partner to the partner site and really push it to its webservices let's say the part

47

00:03:22,720  -->  00:03:29,670
of several host of absurdest ironed destination is one Sandwell directory pushed their data to the partners

48

00:03:29,780  -->  00:03:30,480
absurdest.

49

00:03:30,540  -->  00:03:36,450
OK so those are the two ways in which they can be shared with the partner Web site and exhibitor domain

50

00:03:36,510  -->  00:03:38,670
but the value their destination has.

51

00:03:38,670  -->  00:03:43,300
So let's begin and let's just assume that destination is one.

52

00:03:43,320  -->  00:03:48,300
So in this lesson we will look at only when data is written to the file and through that process we

53

00:03:48,300  -->  00:03:53,700
will learn something about how exceptions are generated are and how that can be handled in the next

54

00:03:53,700  -->  00:03:54,060
lesson.

55

00:03:54,060  -->  00:03:57,420
We will also look at the second approach.

56

00:03:57,480  -->  00:04:02,580
One destination is to one destination is one and we will try to learn a few more concepts but we not

57

00:04:02,580  -->  00:04:09,000
going to do any We are not going to write any code for shading off the data like writing good a file

58

00:04:09,000  -->  00:04:16,080
are calling the passing grade of service that we are not going to do so because that's that's not in

59

00:04:16,080  -->  00:04:17,100
the context here.

60

00:04:17,100  -->  00:04:22,470
So you know our goal is to just learn about exceptions and be I just use using this example as a use

61

00:04:22,470  -->  00:04:22,830
case.

62

00:04:22,860  -->  00:04:23,470
OK.

63

00:04:23,670  -->  00:04:31,000
So let's just go ahead and read this here now when we are if we have to write somebody to a file we

64

00:04:31,010  -->  00:04:36,990
use a class called file output stream which is from the dollar package and is responsible for writing

65

00:04:36,990  -->  00:04:38,360
the report with a fine.

66

00:04:38,370  -->  00:04:43,760
Now if something goes wrong when writing the data then an exception will be generated by the file output

67

00:04:43,770  -->  00:04:44,640
stream class.

68

00:04:44,800  -->  00:04:48,360
OK but the exception will be generated by that particular class.

69

00:04:48,970  -->  00:04:53,750
So if you just use that class you're not going to learn about how an exception can be generated.

70

00:04:53,820  -->  00:04:54,490
Correct.

71

00:04:54,570  -->  00:05:01,510
So the goal for us is to see how we can generate an exception because we are still learning a lot about

72

00:05:01,500  -->  00:05:03,550
the exception generation process.

73

00:05:03,550  -->  00:05:10,640
So let's just assume that we are trying to write the data file and somehow the file is missing.

74

00:05:10,840  -->  00:05:12,420
So we need to generate an exception.

75

00:05:12,490  -->  00:05:14,380
So that's an exceptional situation.

76

00:05:14,410  -->  00:05:18,880
We say that exceptions need to be generated only an exceptional situations.

77

00:05:18,880  -->  00:05:24,730
So in this case the exceptional situation is that there is no file and the file is actually missing

78

00:05:24,810  -->  00:05:27,020
and we cannot write that out with that particular form.

79

00:05:27,120  -->  00:05:29,140
So that's an exceptional situation.

80

00:05:29,130  -->  00:05:32,620
So in this case we need to generate an error and an exception.

81

00:05:32,620  -->  00:05:37,670
So for that we use something called a troll statement and it starts with a rookie.

82

00:05:38,590  -->  00:05:45,080
And we said that an exception is basically an object of the class through mobile or one off it's subprocess

83

00:05:45,100  -->  00:05:45,250
.

84

00:05:45,370  -->  00:05:45,890
OK.

85

00:05:46,120  -->  00:05:53,160
In this case B the subclass is called as a file not found exception and that's the exact class that's

86

00:05:53,200  -->  00:05:56,250
also used by the file output stream class.

87

00:05:56,340  -->  00:06:00,450
OK so if there is any error it generates the file not found exception.

88

00:06:00,490  -->  00:06:05,560
So let's just use that class on gender to an instance of doc of that particular class

89

00:06:08,920  -->  00:06:15,580
and it's giving a compiler error because we need to import it and it belongs to the IO package.

90

00:06:15,700  -->  00:06:16,120
OK.

91

00:06:16,140  -->  00:06:22,100
And it is a subclass off trouble and trouble is the superclass of all off all exceptions.

92

00:06:22,360  -->  00:06:24,880
But it still gives a compiler error.

93

00:06:25,220  -->  00:06:30,970
And that's because this particular method is generating this exception and there is some other code

94

00:06:30,970  -->  00:06:36,280
which is invoked which will be been working this matter and that in working code needs to know about

95

00:06:36,280  -->  00:06:38,210
this particular exception.

96

00:06:38,220  -->  00:06:43,860
OK so here in this case this exception is being generated within the body of the Send method.

97

00:06:43,890  -->  00:06:50,560
So it's an implementation detail but how will the in working code know about this particular exception

98

00:06:50,560  -->  00:06:50,590
.

99

00:06:50,590  -->  00:06:52,210
It needs to somehow know about it.

100

00:06:52,210  -->  00:06:52,790
Right.

101

00:06:52,910  -->  00:06:59,230
And for that we need to indicate that this particular method might throw this exception and that we

102

00:06:59,230  -->  00:07:04,480
need to indicate that here in the in the in the method declaration itself.

103

00:07:04,500  -->  00:07:10,930
So if I hover over it it gives me two options as one handle exception type unhandled exception type

104

00:07:11,410  -->  00:07:16,660
either use surrounded with a quick catch but we will see later or add to this declaration.

105

00:07:16,720  -->  00:07:19,590
And this is what we are going to do not now.

106

00:07:19,620  -->  00:07:25,580
So it I it has added this exception to the declaration and it uses this keyword called Astro's.

107

00:07:25,670  -->  00:07:28,990
OK Andreas can be followed by one or more exceptions.

108

00:07:29,050  -->  00:07:29,600
OK.

109

00:07:29,920  -->  00:07:32,490
So there is no more compiler compilation error.

110

00:07:32,500  -->  00:07:37,240
So in all this and motard is saying that I might throw this exception also.

111

00:07:37,320  -->  00:07:39,630
OK so that's what it is informing.

112

00:07:39,910  -->  00:07:41,310
Any indication.

113

00:07:41,950  -->  00:07:47,520
But if you go back to exceptions demobilize from the sheer method we are invoking the Send method and

114

00:07:47,520  -->  00:07:50,610
this NABC a completion error here also.

115

00:07:50,700  -->  00:07:54,980
And that's because the Send method has to handle this particular exception.

116

00:07:55,480  -->  00:07:58,820
Now if I have got over it we again see the same options here.

117

00:07:58,930  -->  00:08:03,590
It says either our throws declaration or surrounded with a try catch.

118

00:08:03,710  -->  00:08:09,740
OK this time let's pick this up and once we look at that we will look at the first option also.

119

00:08:10,540  -->  00:08:14,310
When I do that we have a try catch block here.

120

00:08:14,320  -->  00:08:20,570
It's also called as an exception handler and the Send method is engrossed in the try block.

121

00:08:20,710  -->  00:08:26,530
And there is a catch block here on the catch block has a parameter here called filing not found exception

122

00:08:26,520  -->  00:08:26,730
.

123

00:08:26,740  -->  00:08:27,270
OK.

124

00:08:27,490  -->  00:08:31,200
So as you can see it looks very similar to we are calling it as a parameter.

125

00:08:31,210  -->  00:08:33,820
So in that sense it is very similar to a matter.

126

00:08:33,830  -->  00:08:34,180
All right.

127

00:08:34,170  -->  00:08:36,490
You can't know within the.

128

00:08:36,550  -->  00:08:38,280
This is the body of the catch block.

129

00:08:38,470  -->  00:08:43,280
And here it's invoking a method called blenched daiquiris.

130

00:08:43,300  -->  00:08:43,790
OK.

131

00:08:43,810  -->  00:08:46,360
So this is the exception handler.

132

00:08:46,360  -->  00:08:52,090
So if you want to write any recovery code when an exception is generated You need to write it here.

133

00:08:52,090  -->  00:08:57,300
So basically an exception will be generated in the Send method and that exception will be handled here

134

00:08:57,310  -->  00:08:57,450
.

135

00:08:57,460  -->  00:09:01,600
So let's just go ahead and run this thing.

136

00:09:02,010  -->  00:09:07,350
So as you can see if we initially were inside main then main is invoking share on Share is invoking

137

00:09:07,360  -->  00:09:08,210
send.

138

00:09:08,640  -->  00:09:11,870
And here you are passing zero.

139

00:09:11,880  -->  00:09:14,680
So this needs to be zero here.

140

00:09:15,590  -->  00:09:17,140
Let me just read on it.

141

00:09:17,590  -->  00:09:20,770
Now you can see that within inside Senator.

142

00:09:20,940  -->  00:09:22,570
So destination is zero.

143

00:09:22,670  -->  00:09:24,700
And this statement is getting executed.

144

00:09:24,900  -->  00:09:31,240
And as soon as this statement is gets executed the control goes back to the to the invoking method which

145

00:09:31,240  -->  00:09:31,910
is share.

146

00:09:32,020  -->  00:09:34,700
So the rest of the method is good to hear.

147

00:09:34,720  -->  00:09:35,400
OK.

148

00:09:35,430  -->  00:09:40,490
And this particular method and off center is not being rendered here only no share on enough meanness

149

00:09:40,500  -->  00:09:41,330
here.

150

00:09:41,350  -->  00:09:43,630
So the control goes back to the calling method.

151

00:09:43,620  -->  00:09:47,960
So this method will be popped off the stack and then controller comes here.

152

00:09:47,990  -->  00:09:53,740
And so then the exception is generated the exception of riposted the JVM and JVM and see if there is

153

00:09:54,660  -->  00:09:56,790
an exception handling code within this method.

154

00:09:56,830  -->  00:09:58,400
I know it's not there so.

155

00:09:58,500  -->  00:10:05,530
So it pops off this method from the from the call stack and here it tries to see if there is an exception

156

00:10:05,530  -->  00:10:08,310
handling code and there is an exception handler here.

157

00:10:08,560  -->  00:10:15,610
So it just it just passes the exception object this particular pyramided it assigns it to this barometer

158

00:10:15,740  -->  00:10:17,380
underpins the message.

159

00:10:17,420  -->  00:10:24,040
And this is nothing but as doctoress basically does it gives information about the type of exception

160

00:10:24,040  -->  00:10:30,090
here which is a file not found exception and it also gives her radius off the exception are sort of

161

00:10:30,090  -->  00:10:34,970
created God generated initially on how that particular method got in.

162

00:10:35,140  -->  00:10:40,460
For example here it says that the exception most generator that has strictly be connect at line pen

163

00:10:40,570  -->  00:10:48,640
within the history class on that particular method was invoked from share method or here at line 17

164

00:10:48,640  -->  00:10:48,760
.

165

00:10:48,790  -->  00:10:49,870
That's very.

166

00:10:50,110  -->  00:10:54,840
This meant that method was invoked and shared was again invoked by another method main.

167

00:10:54,970  -->  00:10:59,790
OK so that's how you have done to try and catch block.

168

00:10:59,800  -->  00:11:01,900
This is how you handle the mechanism.

169

00:11:01,900  -->  00:11:06,280
This is how you handle exceptions and you would have some recovery code here.

170

00:11:06,350  -->  00:11:06,650
OK.

171

00:11:06,670  -->  00:11:12,420
So if an exception is generated it means it's generated in some exceptional situation in which case

172

00:11:12,460  -->  00:11:13,350
we handle it.

173

00:11:13,370  -->  00:11:17,200
And so you would have some recovery code here now.

174

00:11:17,320  -->  00:11:18,400
So there is.

175

00:11:18,400  -->  00:11:22,930
So you had this we thought this strike would be what we were getting a compilation error.

176

00:11:22,960  -->  00:11:30,050
So the next step is if you are not handling it here and if we want to why compiler error then shared

177

00:11:30,050  -->  00:11:33,120
method can actually can actually relate the error.

178

00:11:33,160  -->  00:11:36,100
Which means it says that I'm not going to handle it much.

179

00:11:36,170  -->  00:11:38,510
The Invoking method might be able to handle it.

180

00:11:38,530  -->  00:11:41,680
So I'm just going to relate back with my main working mother.

181

00:11:41,920  -->  00:11:46,310
OK so let's try that option also.

182

00:11:46,330  -->  00:11:53,910
So in this case let's pick the first option as you can see it's the truest gloss has been added here

183

00:11:54,910  -->  00:11:56,660
and the compiler is here.

184

00:11:56,800  -->  00:12:03,550
So let's start on this share method with try catch block knowledge's just go ahead and look at the control

185

00:12:03,550  -->  00:12:05,390
flow.

186

00:12:05,590  -->  00:12:06,730
So you are inside.

187

00:12:06,790  -->  00:12:11,610
Here you are inside us and then an exception has been generated.

188

00:12:11,810  -->  00:12:19,140
And so this statement is not getting executed here Anderson method is bumped off the stack and control

189

00:12:19,150  -->  00:12:27,280
comes back here and to the class and sees that the JVM sees that there is an exception handler here

190

00:12:27,280  -->  00:12:27,690
.

191

00:12:27,760  -->  00:12:31,250
So it's not going to even bring this statement and off share.

192

00:12:31,660  -->  00:12:38,200
And I do not support the sharing method from the call stack and then it will go into the main method

193

00:12:38,230  -->  00:12:41,310
and see if there is an X if there is a class here.

194

00:12:41,350  -->  00:12:46,630
If there is an exception handler here and it is here it's going to print this thing.

195

00:12:46,630  -->  00:12:52,920
And after the catch block then this statement will get executed and off Maine will get executed OK.

196

00:12:53,110  -->  00:12:55,220
So you can see the control flow here.

197

00:12:55,630  -->  00:13:01,420
Now so after the catch block is executed then the statements following the catch block will be executed

198

00:13:01,420  -->  00:13:02,050
.

199

00:13:02,050  -->  00:13:10,270
Now if he'd come and this out the only other thing or only other option as the main method would also

200

00:13:10,270  -->  00:13:12,540
have to throw the exception.

201

00:13:12,670  -->  00:13:17,450
But then in which case this statement will not get executed.

202

00:13:17,670  -->  00:13:24,060
So in none of the statements will get executed because the the keep the JVM keeps popping off those

203

00:13:24,390  -->  00:13:26,450
and those methods off the stock.

204

00:13:26,490  -->  00:13:30,890
And finally it gets here it also pops this off and there is no other method to handle it.

205

00:13:30,940  -->  00:13:32,360
I enjoy them also.

206

00:13:32,560  -->  00:13:35,930
But you know it gives up and the program dominates.

207

00:13:35,950  -->  00:13:41,610
Now if this is if this was a web application with hundreds of threads running simultaneously then one

208

00:13:41,610  -->  00:13:49,000
third will be handling one user request and with in that case if such kind of an exception is generated

209

00:13:49,120  -->  00:13:55,180
if it does not handle finally that that particular thread that belongs to that user will be terminated

210

00:13:55,200  -->  00:13:55,380
.

211

00:13:55,500  -->  00:13:56,000
OK.

212

00:13:56,200  -->  00:13:58,570
So this is what we have here.

213

00:13:59,000  -->  00:14:09,220
And and here one thing I can show is you can also have this and this particular gadget and actually

214

00:14:09,220  -->  00:14:12,500
read toerag even this can be done.

215

00:14:12,610  -->  00:14:15,370
So which means that control comes here.

216

00:14:15,370  -->  00:14:17,660
But then this is going to read through it.

217

00:14:18,400  -->  00:14:25,660
And then it needs to the main hospital still handle it OK or it needs to just you know relay it so you

218

00:14:25,660  -->  00:14:29,450
can also catch block and also to an error.

219

00:14:29,470  -->  00:14:30,940
So that's about it.

220

00:14:30,940  -->  00:14:36,100
So we have looked at the exception generation on handling process and then the next lesson we will also

221

00:14:36,100  -->  00:14:42,430
write the next block here for Destination equal to one and we'll try to explore a little bit more about

222

00:14:42,460  -->  00:14:43,430
exceptions.

223

00:14:43,480  -->  00:14:44,480
OK thank you.

224

00:14:44,490  -->  00:14:48,060
And you can find the court and the resources section for it.

225

00:14:48,160  -->  00:14:54,130
If the files do not how bargello extension and if to not be XDR or some other exception extension just

226

00:14:54,130  -->  00:14:56,670
gone what did Kadar Java and you can compile and run it.

227

00:14:56,820  -->  00:14:57,900
OK thank you.

228

00:14:57,970  -->  00:14:58,940
See you in the next lesson.

229

00:14:58,940  -->  00:14:59,670
Unhappy according
