Not let's look at something Goddard finally broke we have seen how exhibitions can be generated on how 2
they can behind it. 3
We also seeing how exceptions can affect the control for itself that is assoon as an exception is genderqueer 4
rest of the block is squibbed and GBM stops looking for matching exception handler not really such a 5
grow is fine. 6
But sometimes the board might be using some operating system resources in such cases. 7
You would have to become extra care for instance here is I want a sun method from them all for the sake 8
of this discussion. 9
The great catch block is movement with this method from the shared method also some of the code has 10
been omitted. 11
Now if you recall if distintion barometer has a value of zero. 12
We will read the input data are we a local file. 13
And we can use a file open stream class for the final stream would use operating system resources to 14
connect to their destination. 15
These system resources once used to be Glos appropriately to avoid any resource leaks this a medical 16
Glos is in work on the final stream object so the question is valid will replace the last invocation 17
statement so that it gets executed for sure. 18
For instance you may and so did at the end of the play Brock but we know that in this case if an exception 19
is generated let's say when creating a file or part stream object the rest of the try block is on JVM 20
starts looking for a matching exception handler since the block is Glos method will not be invoked in 21
this case. 22
Another option is to place it at the end of the matter. 23
But the problem with that is if an unchecked exception is generated due to whatever reason then it will 24
not be caught by either of these to catch blocks uncontrolled and switched to the calling method unclose 25
method will once again be knocking more among other options you ordered in the first guy you block. 26
But if an exception is generated instead of why not one exception then controlling what goes into this 27
first catch block. 28
And once again grows is not in book know in addition to the first Gutch groc if you also in-circuit 29
in the second catch block then that may still not help if some unchecked exception is generated even 30
if that's not book is The Gordy's cluttered with multiple close calls. 31
So what we need is love grows in vocations statement and it needs to be executed for sure. 32
So we need a got into either an that we can use a finally block in this me. 33
It's an optional block an event present at all residents. 34
It would run even if an exception is generated and the corresponding exception handler is not present 35
in this matter. 36
Only exception would be when GBM engenders a severe error or a medical exit from the system class is 37
invoked. 38
Exit mentor is used to determine the JVM itself which should be done in only some really special cases 39
. 40
As we see in this example finally block usually condense any cleanup or. 41
Another example would be closing of is going to Currence So if you have to do something for sure regardless 42
of whether or not an exception is generated then you would use a fine liberal otherwise you wouldn't 43
need it. 44
Just know that finally block must follow any Gottsch blocks it cannot come in between a cry unaccountably 45
up or in between Bukvich blocks but if not catch blocks exist then it must follow that try block so 46
what that means is a dry rock must be accompanied by at least one catch block or finally broke. 47
Also you can opt out more than one finally block associated with the try block. 48
Not let's look at the control flow when a finally block is in mind letters you'll see how one or more 49
statements that may generate exceptions and these statements are enclosed in their Tricot finally broke 50
. 51
So let's look at the cases when an exception is non-gender get. 52
Also when it is genetic. 53
Krusty is umed that no exception is generated then this is what would happen. 54
Try block will be executed completely which will be followed by the finally block which is then followed 55
by the code that comes after the finally block. 56
But if an exception is generated then the rest of the block is skipped and GBM will check if there is 57
a matching exception handler within that method and if there is a matching exception handler then that 58
particular catch block is executed which will be followed by the finally block which is then followed 59
by any code that comes after the final E-block. 60
But if a matching exception handler is not found then the finally block is executed. 61
But the important thing is in ecord following the finally block is gipped on control which is over to 62
the working mother. 63
And this process is repeated until a matching exception handler is fun. 64
For example if we assume that the invoking meant that also includes a try catch finally block then the 65
rest of the cries give. 66
And if unmatching exception handler is not fun once again then the finally is executed but not any code 67
following it. 68
So that keeps repeating until the matching exception handler is phone. 69
So the point here is if an exception is generated then you cannot exit to any court other than the court 70
in a matching exception handler. 71
And the finally block so that's above the control flow and it's not just this different control focuses 72
in court. 73
So let's switch over to equites OK here we are in the exceptions to mock glass in the sheer amount that 74
we just discussed three control freak cases and we're going to test all three of them so we will at 75
the finally block within this shared method are not in the sun method and the history behind us. 76
So let's just go ahead. 77
Cried the first case which was when an exception is not generated but in other words an exception is 78
always Doniger. 79
So we need to make some minor changes. 80
First thing is let's pass one here. 81
And if you pass one here or destination would be one in an Iowa exception will be generated. 82
But since we don't want an exception to be gender so let's just call him and just out and go back here 83
. 84
And that's put in print statement here let's say after working seven which is good. 85
And that's also how I finally block here. 86
Inside she is finally. 87
So let's just go ahead and run this 88
so as you can see we are going into. 89
There are no exception is thrown so sentiment that executes completely without any shoes and after dark 90
after invoking sense statement as presented here an afterthought after the try block is executed completely 91
and after that the finally block is executed. 92
So it's been doing this. 93
And finally the court after finally block is executed here and Afshar. 94
And we are good with that. 95
So that's the first case and second case was when an exception is generated and a matching exception 96
handler is fun. 97
So let's just uncommon this so an exception is generated and we have a matching exception handler here 98
. 99
OK. 100
So let's just simply exhibit this. 101
So now we're going into insights and then exception is gender Witan doesn't matter. 102
So this statement so the rest of the trial escaped and the catch block is executed. 103
So it Brent's kind of thing different so we're on after the catches executer finally is executed here 104
and after finally the court after finally is executed which is not enough sure. 105
OK. 106
So that's the second case. 107
And third one is the slightly complex case or more interesting case which is an exception is gender 108
but a matching exception handler is not found in the matter. 109
So let's just go ahead and pass zero here. 110
And here for zero I find not one exception is generated. 111
So undrunk comes back here but in the case we've discussed there is no matching exception handler within 112
this method. 113
But in this case we have a handler but we are still retrying the exception. 114
So the control would go here. 115
But then but then we are also throwing it retrying the exception. 116
So it's a slightly more interesting case actually. 117
So we are redrawing it. 118
So in that case with the finally block we'll also the catch block will be exempted or defined and we 119
will be executor. 120
But the court after finally will not be exaggerated as we are reviewing it. 121
So the GBM would look in the main method which is actually invoking the Sheremet there. 122
So let's actually go ahead and try this. 123
So it's zero. 124
Let's put a print statement here also let's just say share Kallen why not for an exhibition. 125
So that's good enough here. 126
And we are throwing an exception to do during an exception there are let's just uncommon this out 127
and let's put statement here after invoking share that meeting Goleman desart so they can grow together 128
. 129
Also come here in this Gottsch block. 130
So let's just say Maine why not for an exception. 131
And let's also put a maned finally here. 132
Students execute that. 133
So we are going into inside saying densify not one exception is generated. 134
So can comes here and do the catch block in share bins this message here share Dunder inside shares 135
finally is executed but important thing is if shared is not printed here ironed. 136
So the controller goes back into the main matter on inside the main method there is a there is a catch 137
block here matching Gutch block so it prints this message here. 138
Main find not one exception. 139
Then it goes into my inside Mayne's finally Anson's a catch block has already been encountered here 140
. 141
It burns and off main mentored. 142
OK but if we are doing here then this message will not be printed. 143
So after INSIDE Mayne's finally the program would actually dominate. 144
OK so that's the only important difference. 145
This particular statement will not be printed. 146
So basically when an exception is generated no code will be executed except for unmatching got a matching 147
exception handler block undefined on the block. 148
All right. 149
This goes on. 150
So that's the third case and that's about it. 151
Please go ahead and try this out and you can also find the good programs in the resources section. 152
Thank you. 153
And happy coding.