1

00:00:01,140  -->  00:00:08,130
Not look at a statement called Try with resources which was introduced in July 7 as the name suggests

2

00:00:08,460  -->  00:00:15,700
it has something to do with the try block and also get resources or resources could be classes to present

3

00:00:15,720  -->  00:00:21,450
something like operating system resources like file handles which has been performing.

4

00:00:21,450  -->  00:00:27,480
I would look at operations like reading from a file on the main thing the statement does is to automatically

5

00:00:27,480  -->  00:00:29,360
close such resources.

6

00:00:29,460  -->  00:00:31,500
So let's go ahead and learn about it.

7

00:00:31,540  -->  00:00:32,980
It is very straightforward.

8

00:00:33,300  -->  00:00:38,060
At the end of the lesson people will also be a short demo of what we like and their demo.

9

00:00:38,070  -->  00:00:44,040
We would also look at something called suppressed exceptions to understand the benefit of the right

10

00:00:44,040  -->  00:00:45,080
with resources.

11

00:00:45,070  -->  00:00:51,180
First let's look at an example here is the standard template which was used before jealous event for

12

00:00:51,180  -->  00:00:54,430
reading it offered him a data hitting the block.

13

00:00:54,600  -->  00:01:00,660
Are creating a file input stream instance which acts as a resource for reading it offered him a fight

14

00:01:01,950  -->  00:01:06,860
the code for reading the dies on the dead as it is not relevant for this discussion.

15

00:01:06,900  -->  00:01:12,460
There is also a catch block for handling file not one exception that can be drawn by the construction

16

00:01:12,470  -->  00:01:15,060
of the file input stream class.

17

00:01:15,090  -->  00:01:20,970
We also have the finally block with the file input stream instance is closed so that the system resources

18

00:01:20,970  -->  00:01:26,720
that it uses on at least this way there will not be any resource leaks.

19

00:01:26,790  -->  00:01:30,790
We already know the finally block will always be executed.

20

00:01:30,870  -->  00:01:32,890
No no it is the finally block.

21

00:01:33,040  -->  00:01:40,180
How one mark like I should talk and that's because the Ruelas method can do an Iowa exception.

22

00:01:40,190  -->  00:01:44,970
Moreover we are also checking that the reference to the above in is not enough.

23

00:01:45,360  -->  00:01:52,320
And if it is not not only then we close the input stream not that available and can be none if the file

24

00:01:52,320  -->  00:01:54,890
input stream object would not be created.

25

00:01:55,050  -->  00:01:59,610
That is a fine enough one exception was generated when we were trying to create dump file input stream

26

00:01:59,610  -->  00:02:00,580
object.

27

00:02:00,960  -->  00:02:07,140
So that this check could get a null pointer exception when the Close method is invoked.

28

00:02:07,260  -->  00:02:13,570
Now as you can see this whole finally block it looks very ugly with the cross try catch block and the

29

00:02:13,600  -->  00:02:17,430
additional check and also the up close method and location.

30

00:02:17,570  -->  00:02:24,630
So I want this we have the quite with resources statement which was introduced in Java 7 and it is the

31

00:02:24,630  -->  00:02:30,400
same code but with that drive with resources statement as you can see it looks much cleaner.

32

00:02:30,660  -->  00:02:37,740
The ugly looking finally block and we have only to try and catch Brocks here the resources are created

33

00:02:37,920  -->  00:02:41,430
Witan the parenthesis that follows the tricky word.

34

00:02:41,970  -->  00:02:48,180
So the declaration is also done within the parenthesis and it will not work if declaration is done outside

35

00:02:48,180  -->  00:02:54,540
of the parenthesis and initialization is done within the parenthesis in the old syntax declaration was

36

00:02:54,540  -->  00:02:56,430
done outside the block.

37

00:02:57,030  -->  00:03:01,880
So a try with resource is the cleanup operation is taken care of implicitly.

38

00:03:02,220  -->  00:03:07,630
That is a close method is in work implicitly after the block is executed.

39

00:03:07,680  -->  00:03:13,600
In reality the compiler inserts the and finally block into the bytecode.

40

00:03:13,770  -->  00:03:18,030
So even if an exception is generated the Close method is still invoked.

41

00:03:18,030  -->  00:03:25,050
Just as in the case of using a finally block explicitly you're also catching an exception as we know

42

00:03:25,050  -->  00:03:32,430
that the Lewis-Smith there which is invoked implicitly can throw an exception just know that the resource

43

00:03:32,430  -->  00:03:40,620
created in the parenthesis is implicitly final so it cannot be reassigned within the try block.

44

00:03:40,620  -->  00:03:43,650
Unsisterly sources are being automatically closed.

45

00:03:43,650  -->  00:03:50,530
This particular feature is also referred to as Automattic resource management or for short.

46

00:03:51,240  -->  00:03:56,690
Here is another example that might deploy resources are being created within the type but and this is

47

00:03:57,400  -->  00:03:59,970
they are created from left Left-Right right.

48

00:04:00,080  -->  00:04:06,940
That resource creation expressions must be operated by semi-colons in disguise syntax wise.

49

00:04:07,020  -->  00:04:14,040
It looks a bit similar to fuddle Not that once the block is completed resources will be closed in the

50

00:04:14,040  -->  00:04:15,660
reverse order.

51

00:04:15,660  -->  00:04:21,810
That is a file output stream instance is close before filing puts team instance even if there was a

52

00:04:21,810  -->  00:04:28,800
failure in creating one of the resources then the resources created so far which are on its left will

53

00:04:28,800  -->  00:04:30,940
be crossed.

54

00:04:31,110  -->  00:04:33,150
And here is a general syntax.

55

00:04:33,360  -->  00:04:39,300
Any resource that we create in the parenthesis must be off by auto plausible which is an interface in

56

00:04:39,300  -->  00:04:42,120
the Jawa line package.

57

00:04:42,150  -->  00:04:47,960
It means that the class of the resource hostile implement Article interface or one of its subintervals

58

00:04:47,970  -->  00:04:55,790
us or disclosable has only one method which is a closed minded support filing stream on file output

59

00:04:55,790  -->  00:05:03,750
stream classes implement articles about not do the try block need not have a catch block if the occludes

60

00:05:03,750  -->  00:05:10,590
method which you implement as part of implementing articles aboon does not throw an exception.

61

00:05:10,620  -->  00:05:14,770
And of course the code of conduct by block should also not to an exception.

62

00:05:15,210  -->  00:05:20,260
So a cry with the social statement need not how either to catch Quarter-Finals block.

63

00:05:20,750  -->  00:05:26,010
But if you want to you can have an explicit finally block that would perform something other than the

64

00:05:26,000  -->  00:05:32,640
cleanup operation Sooke trying with resources would come into play only when dealing with autoclaves

65

00:05:32,630  -->  00:05:36,680
or build resources and hence it is called aspart private sources.

66

00:05:37,290  -->  00:05:42,720
If you're not dealing with all the possible resources then you will have to use the regular old fashioned

67

00:05:42,720  -->  00:05:50,230
type block which does not help but analysis and here are some subtypes of our disclosable on the left

68

00:05:50,230  -->  00:05:50,260
.

69

00:05:50,250  -->  00:05:56,820
We have usable interface which is from the Jawa I will package most of the classes implement this interface

70

00:05:57,560  -->  00:06:02,770
even classes like socket arm scanner which are from other packages implemented.

71

00:06:03,440  -->  00:06:09,480
And we also have the interface of resultset which is from the package job X doc askew and it also extends

72

00:06:09,540  -->  00:06:11,370
our doc Rosabel.

73

00:06:11,430  -->  00:06:17,030
Similarly we can also see that there is a line interface from one of the packages from the sound API

74

00:06:17,580  -->  00:06:19,630
and it also extends our global.

75

00:06:20,120  -->  00:06:23,550
So all of these can be used in the parenthesis.

76

00:06:23,550  -->  00:06:28,610
I would also recommend taking a look at the articles about interface in the J.Y..

77

00:06:29,250  -->  00:06:31,180
So thats a try with resources.

78

00:06:31,380  -->  00:06:38,490
It is mainly used to automatically growth resources unexplored cleanups and backs down using finally

79

00:06:38,490  -->  00:06:40,570
for growing resources.

80

00:06:40,670  -->  00:06:48,360
No look not to pull up my editor you assured them off with resources OK for this demo.

81

00:06:48,380  -->  00:06:54,060
We have this new class called us try with resources gimel and it is added to the exceptions package

82

00:06:54,760  -->  00:06:56,460
and it has only one method File.

83

00:06:56,460  -->  00:07:04,120
Copy that I'm being on stands for automatic resource management it with an Iowa exception and this method

84

00:07:04,120  -->  00:07:05,680
doesn't work from the main method.

85

00:07:05,850  -->  00:07:12,030
And since it throws an exception we are also catching it here now as the name suggests this method is

86

00:07:12,020  -->  00:07:17,610
doing a file copy which means that it is reading the contents of a file and writing those contents for

87

00:07:17,620  -->  00:07:19,240
a different destination.

88

00:07:19,500  -->  00:07:21,150
And that's being done here.

89

00:07:21,210  -->  00:07:25,720
So here we have the private resources index and we are declaring pool resources.

90

00:07:25,760  -->  00:07:27,690
Resources are being created here.

91

00:07:27,720  -->  00:07:32,360
One is a buffered input stream instance and the second resource is a buffered output stream instance

92

00:07:32,370  -->  00:07:32,640
.

93

00:07:32,630  -->  00:07:37,170
So input stream instance is used for reading or stream instance for writing.

94

00:07:37,220  -->  00:07:43,410
Again that's not worry about the code for reading and writing on the Phylis basically a big file here

95

00:07:44,040  -->  00:07:48,210
and they put me as violent or jpeg output would be this.

96

00:07:48,210  -->  00:07:49,990
So here it is valid in Georgia.

97

00:07:50,370  -->  00:07:52,750
So let me just go ahead and run this.

98

00:07:53,390  -->  00:07:56,660
Here does not let me refresh this.

99

00:07:56,670  -->  00:08:02,990
So as you can see the file got created here and here the resources are also separated by a semicolon

100

00:08:03,000  -->  00:08:03,870
.

101

00:08:03,870  -->  00:08:08,030
Now let's try a bunch of things that we discussed on this.

102

00:08:08,040  -->  00:08:13,560
I mean just take this.

103

00:08:17,120  -->  00:08:23,270
Now as you can see we're getting a compilation error here and that's because the resource has to be

104

00:08:23,270  -->  00:08:25,360
created within the parenthesis.

105

00:08:25,500  -->  00:08:29,470
It cannot be created outside and we cannot initialize it here.

106

00:08:29,490  -->  00:08:30,290
That cannot be done.

107

00:08:30,330  -->  00:08:33,900
As we discussed in the in the lesson so that cannot be done.

108

00:08:33,890  -->  00:08:42,120
So let me just bring it back here and one other thing we discussed was that we cannot re-initialize

109

00:08:42,820  -->  00:08:46,190
a resource variable here so we cannot do this.

110

00:08:46,200  -->  00:08:52,520
And that's because we said that in here is implicitly final and final cannot be reassigned.

111

00:08:52,520  -->  00:08:53,650
So we know that.

112

00:08:53,660  -->  00:08:57,250
So that's why this is not possible and hence we can see a compilation error.

113

00:08:57,260  -->  00:08:59,350
So let me just delete this also.

114

00:08:59,930  -->  00:09:02,870
And the next one is.

115

00:09:04,950  -->  00:09:09,000
So here you are trying to create a string instance and it will give a compilation error.

116

00:09:09,170  -->  00:09:14,640
And that's because string is not a resource which means that it does not implement auto Clozaril interface

117

00:09:14,790  -->  00:09:16,280
with the close method.

118

00:09:16,910  -->  00:09:18,220
So that's not possible.

119

00:09:18,240  -->  00:09:23,510
Anything that goes here has to be a resource which implements or disclosable.

120

00:09:23,610  -->  00:09:26,800
Now those are the main things that were discussed in the lesson.

121

00:09:26,970  -->  00:09:30,340
So one thing with which we did not discuss was suppressed exceptions.

122

00:09:30,430  -->  00:09:32,050
And let's look at that now.

123

00:09:32,070  -->  00:09:39,630
So for this let me just copy this here and paste it here.

124

00:09:39,990  -->  00:09:44,540
So these are two instances So basically those are two resources.

125

00:09:44,540  -->  00:09:46,640
One is an instance of class Test.

126

00:09:46,680  -->  00:09:49,140
On the other is an instance of Desta.

127

00:09:49,520  -->  00:09:53,510
And those are defined here at the bottom of this white.

128

00:09:53,630  -->  00:09:58,940
So outside of the tribe with resources class we are defining these two classes.

129

00:09:59,180  -->  00:10:04,670
One is d'Este which implements hardcores a bill and also the Glos method and the close method is throwing

130

00:10:04,670  -->  00:10:06,160
an Iowa exception.

131

00:10:06,410  -->  00:10:09,120
The actual close method in order goes is an exception.

132

00:10:09,140  -->  00:10:12,240
But here we are generating an exception under.

133

00:10:12,250  -->  00:10:18,700
It also recommends that we need to to aspecific exception as possible and in case of the close mentor

134

00:10:18,720  -->  00:10:22,950
is not ruling any exception then we can avoid doing any exception here.

135

00:10:22,940  -->  00:10:24,950
We don't need to how it goes across here.

136

00:10:24,950  -->  00:10:25,520
OK.

137

00:10:25,730  -->  00:10:26,950
So if it is not required.

138

00:10:26,990  -->  00:10:32,330
That's not how it throws across here but it does require quite to keep it as specific as possible.

139

00:10:32,660  -->  00:10:35,130
And this exception this method of throwing exception.

140

00:10:35,150  -->  00:10:40,640
But the message will be an exception on test results implementing or disclosable the Close method.

141

00:10:40,700  -->  00:10:45,250
And it is throwing that exception with the message to be an exceptions based too.

142

00:10:45,290  -->  00:10:47,630
And that's because that it's named us Desta.

143

00:10:47,960  -->  00:10:52,370
OK so the only difference is two as being carded here in the message also.

144

00:10:52,460  -->  00:10:53,730
So that's what we hope.

145

00:10:53,940  -->  00:10:57,240
And here let me come on Das also.

146

00:10:57,860  -->  00:10:58,910
So in this case.

147

00:10:59,000  -->  00:11:01,790
So in the right block we are throwing an exception.

148

00:11:01,790  -->  00:11:06,480
So after doing the copy here we are throwing this exception at the very end.

149

00:11:06,590  -->  00:11:06,960
OK.

150

00:11:07,190  -->  00:11:09,620
So let me just go ahead on her on this.

151

00:11:09,620  -->  00:11:10,490
Come on.

152

00:11:10,910  -->  00:11:17,200
So it will throw exceptions and as you can see so you can see the exception.

153

00:11:17,270  -->  00:11:23,630
So the first the main exception that is being generated is this exception Iowa exception that's maybe

154

00:11:23,630  -->  00:11:26,560
how this exception is getting printed here.

155

00:11:26,570  -->  00:11:31,030
And here is the stockers that you're catching that exception and we are doing it.

156

00:11:31,040  -->  00:11:31,560
No didn't.

157

00:11:31,650  -->  00:11:36,420
The important exception we can see that that is this suppresive exception and that is the second supplement

158

00:11:36,470  -->  00:11:37,760
exception.

159

00:11:37,800  -->  00:11:40,150
So those are generated here.

160

00:11:40,190  -->  00:11:44,800
So once this dry block is executed we know that the resources are closed in the reverse order.

161

00:11:44,810  -->  00:11:51,360
So first it will be closed then it will be closed and will be closed on Wednesday is closed.

162

00:11:51,520  -->  00:11:52,530
The exception.

163

00:11:52,700  -->  00:11:54,350
So that's the real exception.

164

00:11:54,740  -->  00:12:00,850
And that will be encapsulated inside this Aiwa exception as a suppressed exception.

165

00:12:01,250  -->  00:12:09,710
And then after that even DVM tries to close this and also throws an exception and that exception is

166

00:12:09,710  -->  00:12:15,620
the trivial exception that is also and gaps embedded within Aiwa exception has a suppressed exception

167

00:12:15,620  -->  00:12:16,230
.

168

00:12:16,310  -->  00:12:25,010
So we have these exceptions here and it is a nice big it's a nice way to do it because here we are calling

169

00:12:25,010  -->  00:12:31,460
it as an important exception because normally you how only one an exception generated here in the try

170

00:12:31,460  -->  00:12:35,660
block is more important because it will have some important details.

171

00:12:35,660  -->  00:12:38,300
I bought something significant must have happened here.

172

00:12:38,540  -->  00:12:43,460
But what it has when it comes to of exceptions those are those are generated by the rules meant there

173

00:12:43,460  -->  00:12:47,620
is normally you know an exception is non-gendered macros matter.

174

00:12:47,660  -->  00:12:53,090
So it would be something not that important or competitively it will not be as important as the exception

175

00:12:53,090  -->  00:12:55,730
generated from the from within that block.

176

00:12:55,730  -->  00:12:56,870
So that's why it is suppressed.

177

00:12:56,900  -->  00:12:57,660
So I think that's up to.

178

00:12:57,670  -->  00:13:00,810
The exception is not that important exception.

179

00:13:00,830  -->  00:13:06,140
Now here is the abstract trace but I guess if you if the developer or whoever is invoking the client

180

00:13:06,880  -->  00:13:12,110
who is invoking this matter wants to extract the suppressed exceptions then they can do it this way

181

00:13:12,120  -->  00:13:12,310
.

182

00:13:12,500  -->  00:13:16,690
So let me just comment this uncommon this court.

183

00:13:16,730  -->  00:13:22,150
So here we are watching the important exception and we in looking him at their court gets suppressed

184

00:13:22,270  -->  00:13:29,540
which is from the to overlook Iron Age credenza on off two of his circuit and an army officer present

185

00:13:29,540  -->  00:13:30,420
exceptions.

186

00:13:30,490  -->  00:13:31,180
OK.

187

00:13:31,250  -->  00:13:37,080
So so this is added in July 7 just like try with resources Sendak's which was also ordered in Dallas

188

00:13:37,250  -->  00:13:37,830
.

189

00:13:38,210  -->  00:13:41,480
So this is an army that will have two exceptions up as exceptions.

190

00:13:41,480  -->  00:13:43,850
And we are counting on a spending message this year.

191

00:13:43,880  -->  00:13:45,430
So if we are on it.

192

00:13:45,740  -->  00:13:51,800
So we have this so important exception as the main exception under somewhat some exceptions are trivial

193

00:13:51,800  -->  00:13:53,090
exceptions.

194

00:13:53,090  -->  00:13:57,110
Now if this is what the trial would send back to I with resources Sendak's.

195

00:13:57,290  -->  00:14:03,440
But if we just use and not try catch finally block and if we have the same thing happening the resources

196

00:14:03,440  -->  00:14:07,270
throwing exceptions when will that blows matter.

197

00:14:07,310  -->  00:14:13,030
And if you also have this kind of an important exception then the exception that could be determined

198

00:14:13,030  -->  00:14:19,240
by this matter would be one of these the exceptions that are generated from the finally block.

199

00:14:19,310  -->  00:14:25,430
Just recall that in the regular old fashioned try block the Close method to be invoked in the finally

200

00:14:25,430  -->  00:14:26,190
block.

201

00:14:26,430  -->  00:14:31,910
So that gives only the one of the first Groos method were through an exception undocked would be going

202

00:14:31,910  -->  00:14:37,550
by this method on the second exception thrown by the second method and also the important exception

203

00:14:37,550  -->  00:14:38,740
will be ignored.

204

00:14:38,990  -->  00:14:46,220
And that gives the caller just masked masked Dix's exception and you can fix the Massena exception by

205

00:14:46,490  -->  00:14:48,190
making it a suppressed exception.

206

00:14:48,230  -->  00:14:50,520
We need to write some additional code for that.

207

00:14:50,720  -->  00:14:55,880
And that's what even the with resources index is doing internally and we can do that to fix the mast

208

00:14:55,880  -->  00:15:00,500
exception problem they ignored exception rubbed up on what we've been seeing the in the next lesson

209

00:15:00,690  -->  00:15:01,070
.

210

00:15:01,370  -->  00:15:02,630
So that's about it for now.

211

00:15:02,660  -->  00:15:03,280
Thank you.

212

00:15:03,380  -->  00:15:04,880
And happy coding.

213

00:15:05,030  -->  00:15:08,590
And you can also find discord in the resources section.

214

00:15:08,600  -->  00:15:09,020
Thank you
