2. So…. what is EXCEPTION HANDLING ?
Exception Handling is the process of handling the expected or
unexpected exceptions.
You can even raise a user defined error message.
There are several Exception Handling specific keywords in
python.
For Exception Handling you don’t need to import any internal
or external library or module.
3. Using Exception Handling we can avoid unexpected termination during
code execution process and most importantly avoiding those scary looking
error messages.
For example :-
below there are two image of same error(ZeroDivisionError, which
happens when we try to perform division by zero) (a) is with out
exception handling whereas (b) is with exception handling.
We can see the difference between two (b) much more friendly whereas
(a) is nerdy.
Why do we need Exception
Handling ?
(a (b
4. What is an Exception ?
Exceptions are a means of breaking out of the normal flow
of control of a code block in order to handle errors or other
exceptional Conditions.
An exception is *raised* at the point where the error is
detected; it may be *handled* by the surrounding code
block or by any code block that directly or indirectly invoked
the code block where the error occurred.
5. The hierarchical representation of EXCEPTIONS is below :-
**Note**
Whenever an
exception is caught by
the handler it will
return exception class
instance according to
the **except** clause.
Which means other
than just the exception
class name it will also
contain the stack
traceback, except
when the exception is
"SystemExit".
6. How do we handle exceptions ?
In python we some special keywords to implement the exception
handling, which are as follows :-
Try : *try* statement is the start of exception handling block.
Except : *except* statement is the end of exception handling block.
Raise : *raise* statement allows user to raise exception.
Finally : *finally* statement is used as clean up section for safe
termination.
Lets look at each of them with some code………….
7. Basic exception handling code looks like
this ..
Now if we try to run it, it will ask us to enter a number but u can enter
an alphabet too in which case it will cause the error while converting
alpha character to integer which will look like this ..
So what actually happened here is that it asked a number and we
entered non numeric value to it which caused a runtime error or an
exception.
8. In previous example it was pretty obvious about the type of error but
what happen if there are multiple statements inside ‘try – except’
block.
Now if we run it again we will
need to enter a value and If we
enter an non-numeric value
result will be “ValueError”
whereas on entering a numeric
value doesn’t give anything
except if we enter ‘0’ which will
return “DivisionByZeroError”
just as shown in the image.
**Note** Some() function is
same as code above.
9. There is a add on to this try-except block which is capability of using else
and finally keywords.
Finally keyword is used as cleanup section it runs regardless of whether
‘try-except’ block has caught any error or not, except when any syntax error
or forced termination occurs.
Here is how it looks like in code ..
10. The else statement executes if no exception has been caught by ‘try-except’
block.
The use of the else statement with ‘try-except’ block is better than adding
additional code to the try clause because it avoids accidentally catching an
exception that wasn’t raised by the code being protected by the ‘try-except’
block.
In the right most image we some
stack traceback report telling error
with line number, here during
handling of ‘ValueError’ another
error occurred which wasn’t handled
by ‘try-except’.