[no audio]
Friends here we are going to
see the difference between 'try except finally'
and 'try except else' block. See already
we know simple 'try' and 'except'; and 'try except Exception as e:',
and also by adding known exceptions, right.
See I mean, let me open my editor and I am going to save
it as a simple script 'differences'.
Yeah, I will take this one.
[no audio]
Now see that. So already we know if I try to print something
called variable 'a', right,
[no audio]
Simply I am printing,
[no audio]
"Exception occured".
So guys, I am trying to print variable value 'a' but if that
is not defined definitely you'll get an exception.
That's why I'm handling in this way, instead of getting an
error. It's correct.
But the thing is what type of exception you are getting.
So for that what you can do, you can add 'Exception as e' statement,
and then you can print that error as well.
Yes, I'm getting that. But if name is not defined, you know
that what is the exception your Python is going to raise, that
is 'NameError'.
That's why I can handle known errors in this way
also. Before your 'except Exception as e' block
you can write in this way. Likewise any, right, any exception
you can handle if you know the exceptions.
"Variable is not defined". Now, which block is going to execute?
[no audio]
Right. This block is going to execute. Suppose if 'a' is defined
then there is no exception,
right? If 'a' is defined, no exception. Simply you are printing
'a' variable value.
It may be any value, right?
[no audio]
That's fine.
But if 'a' is not defined then only your Python is going to
raise an exception, and if that exception is there here, then
it will match and it will execute that 'except' block.
That's fine.
Likewise you can handle any number of exceptions if you know,
and we know we have 'finally' as well.
What is the usage of 'finally'?
"This will execute
[no audio]
always".
Let me run it.
[no audio]
See as of now 'a' is 10 and you're trying to print 'a' variable
value, so there is no exception.
So your 'try' block is success. If 'try' block is success, you're
not getting an exception. That's fine.
But if 'finally' is there, whether you are going to execute your
'try' block successfully or not, or whether you are going to
raise any exception or not,
'finally' will always execute.
So whatever the code is there in 'finally' that will always
execute. That's fine.
Now, what I am doing is, let me comment this.
[no audio]
Now I am going to write 'else' block.
So I am writing simply 'print(
[no audio]
"This will execute
[no audio]
if there is no, if there is no exceptions
[no audio]
in try block". That's it.
Simple. 'finally' is always executing,
but whereas if you add 'else' block after all your exceptions
in your 'try' block what will happen means,
if there is no exception, if everything goes good with your
'try' block then only 'else' will also execute. See as of now,
everything is good.
I mean 'try' block is good, right. There is no chance to
get any exception because 'a' is there and you're trying
to print 'a' variable value.
[no audio]
Now this will execute if there is no exception
in your 'try' block.
Now, what I am doing is, I am deleting 'a' variable value.
Now, you are going to get an exception "a is not defined", that
is 'NameError', "Variable is not defined".
So if you get any exception in your 'try' block, then your
'else' won't execute. Now see the result.
You're not getting this message, 'else' block message.
So simply if this is success,
right, then only 'else' will execute. If 'try' will give
you any, suppose if any exception occurred in 'try' then 'else'
won't execute.
Right. Fine.
So, you know the difference 'else' and then 'finally'. And guys,
you can also include after 'else', 'finally' also. That means
actually 'try', 'except', any number of 'exception' blocks, then 'else'
and 'finally' is the complete syntax if you want. So 'finally'
and 'else' are optional. These two are optional, if you want
you can include it.
Right. Now see I'm running.
[no audio]
What will happen?
[no audio]
'print(a)'. Some exception occurred, that is 'NameError'.
So that's why "Variable is not defined", you are printing. So
if exception occurred then anyway 'else' won't execute.
So 'else' message you are not getting, but 'finally' you are getting
because 'finally' will always will execute that.
Now I am defining 'a' variable value. Now
I'm running. See the result.
So there is no exception in your 'try' block,
that's why it is successful. Your 'try' is success. If 'try' is success
'else' will execute.
Yes, you are getting. Anyway, 'finally',
'finally' will always execute.
That's what it is doing.
So this is the complete syntax for your 'try, except, and else,
finally' block, right?
So guys for time being try to remember the syntax if you
write in this way what will happen. At what time, at what
block is going to execute, right.
So once we start with real-time scenarios there I will try
to use this effectively, up to that I can't use it. For time being
try to remember these syntaxes.
Okay. Okay guys,
Thank you for watching this video.
[no audio]