0% found this document useful (0 votes)
2 views

Exception Handling in Python

The document provides an overview of exception handling in Python, explaining what exceptions are and how they disrupt program execution. It covers various types of exceptions, the use of try-except blocks for handling them, and the implementation of finally and else blocks. Additionally, it discusses raising custom exceptions and the use of assertions for debugging purposes.

Uploaded by

moredattatray526
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Exception Handling in Python

The document provides an overview of exception handling in Python, explaining what exceptions are and how they disrupt program execution. It covers various types of exceptions, the use of try-except blocks for handling them, and the implementation of finally and else blocks. Additionally, it discusses raising custom exceptions and the use of assertions for debugging purposes.

Uploaded by

moredattatray526
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Exception Handling using Python

Tushar B. Kute,
http://tusharkute.com
Exception

• Exception can be said to be any abnormal condition


in a program resulting to the disruption in the flow
of the program.
• Whenever an exception occurs the program halts
the execution and thus further code is not
executed. Thus exception is that error which python
script is unable to tackle with.
• Exception in a code can also be handled. In case it is
not handled, then the code is not executed further
and hence execution stops when exception occurs.
Exceptions

• ZeroDivisionError: Occurs when a number is


divided by zero.
• NameError: It occurs when a name is not found. It
may be local or global.
• IndentationError: If incorrect indentation is given.
• IOError: It occurs when Input Output operation
fails.
• EOFError: It occurs when end of file is reached
and yet operations are being performed
Uncaught Exception
Python’s exception handling
try… except

• The malicious code (code having exception) is


enclosed in the try block.
• Try block is followed by except statement.
There can be multiple except statement with a
single try block.
• Except statement specifies the exception which
occurred. In case that exception is occurred, the
corresponding statement will be executed.
try… except

• Handling the exceptions:


try:
code to monitor for exception
except exception-name:
message to print
Using try… except
Still … uncaught exceptions
Handling multiple exceptions
Handling all exceptions
Handling exception with messages
Finally block

• In case if there is any code which the user want


to be executed, whether exception occurs or
not then that code can be placed inside the
finally block.
• Finally block will always be executed
irrespective of the exception.
• Generally used for shutdown activities like
closing the file, terminating database
connections etc.
Example: finally
The else block

• The try statement can have a else block too.


• The else statements will be executed when
except is not executed.
• Either except (in case of exception) or else (no
exception) blocks are executed.
Complete exception handling
try...except...else...finally
Raising an exception

• We can use raise to throw an exception if a


condition occurs.
• The statement can be complemented with a
custom exception.
Example: raising an exception
Creating your own exception

• Sometimes you may need to create custom


exceptions that serves your purpose.
• In Python, users can define such exceptions by
creating a new class.
• This exception class has to be derived, either
directly or indirectly, from Exception class.
• Most of the built-in exceptions are also derived
from this class.
Negative Number Exception
Assertions

• Assertions are statements that assert or state a fact


confidently in your program.
• For example, while writing a division function, you're
confident the divisor shouldn't be zero, you assert divisor is
not equal to zero.
• Assertions are simply boolean expressions that checks if
the conditions return true or not. If it is true, the program
does nothing and move to the next line of code. However,
if it's false, the program stops and throws an error.
• It is also a debugging tool as it brings the program on halt
as soon as any error is occurred and shows on which point
of the program error has occurred.
How assertions work ?
When to use assert ?

• In Python we can use assert statement in two


ways as mentioned above.
• assert statement has a condition and if the
condition is not satisfied the program will stop
and give AssertionError.
• assert statement can also have a condition and a
optional error message. If the condition is not
satisfied assert stops the program and gives
AssertionError along with the error message.
Example:
Thank you
This presentation is created using LibreOffice Impress 5.1.6.2, can be used freely as per GNU General Public License

/mITuSkillologies @mitu_group /company/mitu- c/MITUSkillologies


skillologies

Web Resources
https://mitu.co.in
http://tusharkute.com

[email protected]
[email protected]

You might also like