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