Let's not continue without them all earlier we have seen how an exception can be generated on how it 2
can be handled it's not on a couple of more things first is about having more than one exception in 3
the last. 4
OK. 5
So trust Roesgen how multiple exceptions. 6
So we will look at that. 7
And the second is that an exception object can be referenced. 8
Polymorphically. 9
OK that's possible. 10
And that's because an exception is basically an object. 11
OK. 12
So we will also look at. 13
So let's just go ahead and extend the cord. 14
Destination is equal to one so we said that if destination is Vaun it means that we want to pass the 15
date up programmatically to the park nursery. 16
So it's OK. 17
So that's what we're going to do. 18
And if the partnership is done that is it is unreachable then it would throw an exception called us 19
Iowa exception OK which is also from the Gelada art IO package even file not found exception was from 20
the Java dot IO package but that's an exceptional situation. 21
Typically you don't have so much going on often. 22
So that's an exceptional situation. 23
And when that happens we need to raise an exception. 24
So let's just do that let's just throw new your exception. 25
OK. 26
Now we need to import this Naga second exception as we know that it needs to be mentioned here. 27
So let's have it here be a good Let's go back here to exceptions Demel now send mattered also needs 28
to handle that exception. 29
OK so when behavior it shows four options let's pick the second one which says I got glass to surrounding 30
you so there is a song on and try on we want to get Gutch glossier. 31
OK let's do the now and start off here. 32
So we have Iowa exception now. 33
So let's put a print statement here. 34
Because in the catch block we are going to how exception handling code. 35
And for now this is our exception handling course so this is how we recover. 36
Saw this you are just saying connecting to a different server. 37
Ok so that's how we're going to recover from this exception. 38
So let's just go hard and change this to one here. 39
Now and it's just you're on it. 40
So we are inside main inside share inside San and then an exception was generated and then we and then 41
the controller came here into Iowa exception under sprinting connecting to a different server. 42
And once that is done and share is printed on this particular matter has popped off the stock. 43
And then finally and off Manus Bernard Here. 44
So that's multiple exceptions. 45
So you can have it within the glass you can have multiple exceptions. 46
So that's the first thing. 47
And the second thing is so we know how we know how to objects here exception objects are the exception 48
objects can be referenced polymorphically. 49
Now in this case file not an exception is actually a supperless off Iowa exception. 50
OK. 51
So file not for an exception can be referenced using IO exception. 52
OK so that's possible. 53
So we are going to reference it polymorphically on what that means is we don't need both of them here 54
so we can just remove this year and it will be polymorphically referenced by Iowa exception so you don't 55
see any compilation error. 56
Now the same can be done here also. 57
So we don't need to catch blocks here. 58
So that's just common default and let's change it back to zero soil file not for an exception is done 59
record but it will be caught inside an exception handler which has no exception. 60
OK so let's just go ahead and run this now you can see that we are inside this exception Rocklynne file 61
not for an exception has been generated. 62
It's generated because we are passing zero but we shouldn't be doing this often. 63
In this case we shouldn't be doing it because you'll see it's a file not found exception. 64
Unbe are saying that we are going to get a different server. 65
So the court has to be specific defined not for an exception. 66
So in this case. 67
But I like ception objects and I find not for exceptions both of them have the same put it over the 68
court and we don't want to do that. 69
So sometimes it is fine. 70
But generally you would want to how specific to cover the court. 71
So let's just change it back here. 72
So this is how it needs to be k so that very well how specific error handling Goertz not and another 73
thing is here file not found exception is a subclass of this. 74
So these exception handlers should be placed from Subclass to words superclass So if a subclass exception 75
handler should come before a gross exception handler. 76
Otherwise we get a compiler that. 77
Let's actually try that k now you'll see that as a compilation error on it says unreachable catch block 78
for filing for an exception. 79
It is already hundred by the catch block for Iowa exception. 80
So you cannot do that. 81
So let's just say control-C. 82
So this is how it should be. 83
And so that's about polymorphically referencing them. 84
Now you can also how other exceptions like for example both fighting not for an exception are no exception 85
are subclasses of this classical exception so you can also have dark and exception is actually a direct 86
supperless of true love. 87
So you can also have trouble here. 88
So both of these are possible. 89
OK so you can do that. 90
So that's all because off exception objects can be referenced polymorphically as an exception an exception 91
is basically an object. 92
OK. 93
And another thing is we said that it goes from subclassed to supercross here. 94
But let's say if the exceptions are siblings. 95
As long as they don't how would the exception clauses do not have a superclass oppressive relationship 96
. 97
Then we could we can have them in any order. 98
OK. 99
Only thing is if they have a supercross up close relationship on it only then the supperless should 100
come before supercross. 101
OK so as long as it's within the exception inheritance tree that starts with the Crucible as long as 102
they are not in the same inheritance but we are good so they can be in any in any order. 103
And so that's about it. 104
Now the cool thing about this whole exception handling mechanism is all you are exception and exception 105
handling code is one place. 106
OK so you don't it's not scattered across the hall and outsource scored and then a class. 107
So it's all in one place of Fard you know for the gross statements. 108
So you can you can always you know with a quick glance you can you you know what kind of exceptions 109
can be generated. 110
OK so that's one nice thing. 111
And the second thing is compiler is actually forcing us to handle the exceptions. 112
OK. 113
And it is not making the programmer to handle it as an afterthought if it is like an afterthought. 114
Then the programmer would be lazy and really not might and most likely not provide the exception handling 115
code because he or she might forget about it. 116
So that that would lead to for a trial record. 117
So because handling except exceptions is basically an exception in situations happen in exceptional 118
situations on computer wants us to handle it. 119
OK. 120
Now sometimes programmers how something like this. 121
Normally it is by convention a lot of people use only exception procedure but it is. 122
But as we mentioned earlier we need better re how specific exception process. 123
But when they do how sometimes they're just don't provide any body within the catch block you know they're 124
just how empty balances on that is also not good. 125
So so in a way you're kind of cheating it cheating the compiler itself. 126
So that's not good. 127
And there is an item specific to that it would be to look at that later but it mainly says that we need 128
to have some kind of audit already called but we will look at that item later. 129
So the compiler is basically checking that the truest clause includes the exceptions here. 130
If you have some kind of a troll statement here if you're throwing some exceptions it is ensuring that 131
we how it checks that it checks that the exception is specified here in their declaration. 132
And it also checks that the exception is caught somewhere. 133
OK. 134
So there is an exception handler. 135
So it's going to check it's going to do that kind of a checking for these exceptions. 136
And for that reason all these exception classes are referred to as checked exceptions as they're being 137
checked by the compiler. 138
So what that means is there are also other types of exceptions which are not checked by compiler. 139
So which means that you can throw an exception but you need not specified them in the declaration. 140
Similarly you need not catch them also. 141
So there is no option on it to catch them. 142
So those are all different types of exceptions on there since they're not being checked by the compiler 143
that cardless unchecked exceptions. 144
OK. 145
Undoctored that is something that we would look at in the next lesson. 146
OK. 147
So that's about it for now. 148
Thank you. 149
And happy coding and I'll see you in the next lesson.