[no audio]
Friends here we are going to
discuss about exception handling with your
Python. First of all, what is an exception? See simply, simply
exception is nothing but an error and a Python program or
script terminates as soon as it encounters an error. Assume
that you have some ten or hundred or thousands of lines in
your Python Script.
[no audio]
Maybe you have somewhere in your code error.
If that error is encountered by your Python,
then your script will stop. Your script won't execute.
It will terminate, right.
See let me show you with simple example.
Let me save it as 'working_with_exceptions.py'.
Now guys, first of all I am writing simply, see that
[no audio]
"Welcome to
[no audio]
exceptions concept".
[no audio]
Fine, let me run it.
Yes, you are getting your output. Right. Assume that you
have only one line in your code. No problem.
Let me remove this parentheses at the end of your 'print'. Now
see the result. You're getting an error. Because of that error
you are going to stop your Python Script.
Assume that you have one more line then. I will do one more
thing. First of all
I'm having.
[no audio]
Now it is fine.
Let me save it and run it.
Yes, you're getting two lines of output.
But in the very first line what I am doing is, I am doing a mistake.
[no audio]
Now, you're not getting output for
second line
[no audio]
See, while completion of your first line you did some mistake, right,
because of that output, what is output you're getting?
"SyntaxError". So you have some error somewhere. Because of that
error your Python is going to stop your script. Right. Fine.
Now, let me do it. Now, it's fine.
Now, what I am doing is, 'print(, some 4/, or 4/2.
Yes, you're getting result as 2.0. That's fine.
Now instead of 2 I'm taking 0.
Now see the result.
[no audio]
Again, you're also getting an error. That error is 'ZeroDivisionError: '.
'division by zero'.
See guys, if you compare in this way, let me remove here.
What is output you're getting? 'SyntaxError'.
Now if I do this thing, I mean divisible with 0,
now you are getting 'ZeroDivisionError'.
See previously you are getting 'SyntaxError', but now you are getting
'ZeroDivisionError'.
So basically errors are two types.
In your Python,
errors are two types.
What they are?
First one is
'SyntaxError' or 'SyntaxErrors'
Second one is, see guys,
if you observe 4/2, it's working perfectly.
That means perfect syntax
is there. If I do with 0 as per syntax, it's correct, right?
Maybe as per your mathematic rule it is not possible,
but as per syntax it is correct.
Now, while running you are getting an error because of 4
division with 0 is not possible.
That means while running you're getting a different type
of error called 'ZeroDivisionError'.
Now this type of errors are called runtime errors.
See as per syntax it is correct, but while running because you
don't have any defined value for 4/0, right,
that's why you are getting an error.
That's why this is a runtime error.
[no audio]
And these errors are nothing but exceptions.
[no audio]
This is an exceptional case, right. As per mathematic rule
if 4/2 is possible,
sorry 4/2 is possible but only 4/0
is not possible, some exception.
So exceptions are occurring only during runtime, right. Fine.
See this syntax errors,
you don't have any special rule to handle this syntax errors.
That means you need to follow the syntaxes exactly while
writing your Python Script. No way
to handle syntax errors, but you can handle exceptions. You
can handle exceptions. That means, see as of now you're getting
suppose 4/0, some 'ZeroDivisionError'. That type
of errors you can handle and instead of terminating your
Python Script in this way
you can terminate with meaningful information.
[no audio]
Right. So I will show you that. Before that there is a way
to handle,
[no audio]
it's important guys, to handle runtime errors or exceptions.
Let me show you with simple example.
See what I am doing is,
[no audio]
don't worry about syntax.
Just observe the output.
I am printing simply 4/2.
[no audio]
Now I am simply printing.
[no audio]
Now previously you are getting your output in this way, some random
message you are getting. Now you are not going to get. You
are going to get. See that result first.
It's perfect. Suppose if I do 4/0, what is happening?
It is not giving an error.
Of course, this is the error but we are handling that. We are giving
with clear information 'ZeroDivisionErrors', or
'ZeroDivisionError'. Now it is not like an error. Of course
it is an error, but we are handling it carefully. Right. Fine.
So first of all
[no audio]
How, when, when you have to use exception handling
in your Python Script?
Nothing is there guys. Very simple. Assume that you have a
code. Assume that you have a code, and if there is a chance
to get any runtime error, then you have to handle it.
Very simple thing. One more thing.
Let me do one thing.
I want to open a file.
I want to open a file called
let's say 'open nari.txt', for read purpose.
That's my default mode
I am not mentioning. Then I am closing. Between these two
let me print 'fo.read'.
See the output. What you are getting.
'FileNotFoundError'.
Actually 'nari.txt' file is existing,
then you don't have any error. As per syntax it is correct,
but while running because there is no file you are getting an error.
That's why this is a runtime error.
So here there is a chance.
What is that chance?
Maybe 'nari.txt' file exists or may not exist.
Right. So that's why you can expect here,
yes, there is a chance to get an exception.
That's why you can handle in this way.
What is that?
'try:'. 'try:', open your file,
[no audio]
then let me print content,
[no audio]
then let me close our file. If really file is there this code
will execute otherwise
'except:' let me write some, I don't know
what is the error.
Let me write, "Some problem while reading the nari.txt
file". That's it.
Now, let me run it.
[no audio]
Yeah, now it is 'SyntaxError'.
What is that syntax error?
[no audio]
Print. Yeah, it's not 'for', right. It is simply 'fo'. Yeah.
You can't handle syntax errors.
So be careful while writing your syntaxes.
You can handle only your runtime errors.
That's what here I'm showing. See actually 'nari.txt'
file is not there.
If it is there what will happen? This block will execute. Then
it will simply skip this line, simply 'except' block, it will
simply skip 'except' block.
If something goes wrong here while running your codes, then
your Python will stop this part and then it will try to execute
the code which is there in your 'except' block.
So it's like a simple thing like if and else but you know
in if and else what will happen? If 'if' condition is false, then
only 'else' will execute but here not like that.
Your Python will try to execute this block first. In this
maybe at any line
if something goes wrong, then your Python will stop the execution
of this block and immediately
it will try to execute the block which is there under your
'except'. That's it.
[no audio]
Right. See now, what I am doing is I don't know
what is the error I'm getting as of now.
That's why you can also get what type of error you are going
to get here 'except Exception',
what is the exception?
[no audio]
as some error 'e', and you can print that
[no audio]
because of this error you are stopping, your Python is going
to stop your code. See that, 'No such file or directory
called 'nari.txt'.
That's it. Actually this is a 'FileNotFoundError'.
[no audio]
If you don't handle with the 'try' and 'except' then see instead of
[no audio]
in a smooth way you are handling your code,
if you use 'try' and 'except'. If you don't use that what will happen?
[no audio]
See that. Simply I'm doing this, no.
[no audio]
You're getting like 'FileNotFoundError:
No such file or directory'.
Of course, the error is fine,
but you are not handling properly to run your code.
That's the reason whenever there is a chance to get an exception
while running your code, at that time
please try to use 'try' and 'except' block.
And, these are simple thing guys, 'try' and 'except' block.
So let me write how to handle
[no audio]
exceptions. Simple, it is an interview question, guys. With the 'try'
and 'except' block. That's it.
[no audio]
Right. Then, what is the syntax for that?
Very simple.
'try:', whatever the code you have just try to write in a 'try:'
block. Let me write some line number 1 or statement 1. You
can write any number of statements under this 'try:'.
'statement-3', something like that.
If something goes wrong with any one of the statement which
are there under your 'try' block then your Python will
automatically skip the 'try' block and then it will try to execute
'except:' block. In 'except:' block you can write something.
So, "This is
[no audio]
because of something
went wrong
[no audio]
in try block".
So what is that? This is just to tell that something goes
wrong in your 'try' block.
But what I want is I want to see exactly what is the error
is there, then what is the syntax we have to use
is, same syntax. Actually now, this is useful guys.
This is useful in your real time.
[no audio]
But this is simple way. We will see advanced level as well.
'except Exception as e'. So why I am taking 'e', you can take
anything, but this is an error
that's why just to represent that I am taking 'e' variable.
Right. Now, instead of this message
what I can write is
See, "This is because of", "This is",
Now you know what is the error?
I mean your Python knows that. Your Python handles that with
variable called 'e' with the help of 'Exception' clause here.
"This is because of", right, so you can write simply this way. That's it.
[no audio]
Right. Okay.
[no audio]
Now, let me show you one more thing.
I am going to comment entire code.
[no audio]
See, what I am doing is
I have a list called
'my_list='
some values [3, 4, 5], and guys if I try to print 'my_list
[1], or maybe 2, whatever it may be, you're going to get
your output, right?
'my_list[1]' is nothing but 4.
But if I try to print 'my_list[4]', what is the result you are getting?
As per syntax it is correct,
but 'my_list[4]' is nothing but 5th value from your list.
So we don't have five values in our list.
How come this fourth value, fourth index value we can get?
It's not possible, right?
That's why what are you showing? 'IndexError: list index out of range'.
[no audio]
Right. See, what I will do is,
I will try to print 'my_list' index of 4, in case if, I don't
know how many values are there in 'my_list', just assume that. Somewhere
'my_list' is there. I don't know. While writing our course you
will come to know how many values. Sometimes you need to create
dynamically your list based on some values.
You don't know how many values are there.
Logically, you need to get suppose some 5th value then
what I will do is, I will try to print 5th value in this way,
but there is a chance that is, you don't have any values inside
of your list. Maybe you have only two values but you are trying
to access 5th value.
That's why what I am doing is
'except Exception as e', then
I am trying to print that 'e'. So that instead of getting an
error, I can see some message. Of course,
if you get an error you can, you're also going to see message,
but the problem is you're going to terminate your script.
But with exception handling, you're not terminating your script.
See that, after 'try' and 'except', I am writing
"This code will also execute".
[no audio]
Now see the result. You are getting an error like 'list index out of range'.
This code will also execute. But instead of that, suppose
if I don't have 'try' and 'except' block, and if I try to print
directly from our list a value called 5th value see that what
will happen. 'print(my_list[4])' I'm trying to print. See
[no audio]
index, 'list index out of range', and your Python
program is terminating here itself.
It is not trying to execute this one.
[no audio]
See actually do we have any dependency
between this line and this line?
No. Because here you are using list, but nowhere we are
using list in 39th line.
If you are not using then no problem,
you can execute this line, no.
Why should we stop your code?
That's why, that's the reason we are using 'try' and 'except'
block in your code.
[no audio]
Right. Fine. So simply.
Yeah, see here.
[no audio]
This is the syntax, right.
So 'try:', whatever the main code you have to run that code
you just try to write under 'try:' block.
[no audio]
So in case if something goes wrong while running your code,
then what you want to display, then that you have to write
under 'except:' block.
Right. So this is just simple thing to handle your 'try' and 'except',
I mean to handle your exceptions, but you need to use 'try'
and 'except' as 'except Exception as e' then try to print that
error. This is useful.
[no audio]
Right.
This one is important one.
That's fine.
Now, let me show you some of the examples where you are going
to get exceptions. 'IndexError',
we have seen that. 'ZeroDivisionError',
we have seen that. 'ImportError', 'ValueError'.
What is 'ImportError' means, just assume that you're trying
to import some module,
so if that module is already there with your Python as a
default module, then you won't get any exceptions but assume
that you want to read Excel sheets, to read Excel sheets suppose
you need to use 'xlrd' module.
This is not the default module with your Python, then
while importing this module you will get an error, right?
So whenever you are trying to import third-party modules,
you need to use 'try' and 'except' block.
Let me show you that as well.
It's important one.
So what I am doing is, let me comment once again your code.
[no audio]
Just assume that I need to import a module called, suppose
some 'fabric'.
[no audio]
Let me run it.
There is no module, right?
No module named 'fabric'. 'ModuleNotFoundError'.
[no audio]
Right. See that. What I can do is in that case 'try:'
'except Exception as e', then I am printing simply error.
So instead of terminating by your Python
we are terminating, we are stopping or skipping gracefully,
right. So that is the way to use your 'try' and 'except' block. That's it.
So I am giving just some of the errors - 'TypeError', 'NameError',
'FileNotFoundError', something like that. Okay.
So while going forward we will see different types of errors
and how to handle them. And we have one more video on this just
to handle directly the known errors, right? Okay, guys.
Yeah, one more thing, 'IOError'.
We will see them, all of them while going forward.
Okay, don't miss next video, okay.
Okay guys, thank you for watching this video.
[no audio]