0% found this document useful (0 votes)
4 views23 pages

Chapter 01 - Exception Handling in Python

The document explains exceptions in Python, which are events that disrupt the normal execution flow of a program. It describes three main types of errors: syntax, runtime, and logical errors, and outlines how to handle exceptions using try, except, else, and finally blocks. Additionally, it covers the use of raise statements and assert statements for manual exception handling and testing expressions.

Uploaded by

hiya.ladani
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)
4 views23 pages

Chapter 01 - Exception Handling in Python

The document explains exceptions in Python, which are events that disrupt the normal execution flow of a program. It describes three main types of errors: syntax, runtime, and logical errors, and outlines how to handle exceptions using try, except, else, and finally blocks. Additionally, it covers the use of raise statements and assert statements for manual exception handling and testing expressions.

Uploaded by

hiya.ladani
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/ 23

Classification: Internal

Classification: Internal

An exception is an event, which


occurs during the execution of a
program that disrupts the normal
flow of the program's instructions.

In general, when a Python script


encounters a situation that it cannot
cope with, it raises an exception.

An exception is a Python object that


represents an error.
Classification: Internal

There are three main types of errors (exceptions) a python program may
encounter. These errors are follows.

Types of
Errors

Syntax Run Time Logical


Errors Errors Errors
Classification: Internal

The try block contains code, that is to


be executed or tested.

In case there is an exception (error) in


any of the lines of code then except
block will be invoked and executed.
There can be multiple except blocks.

In case there is no exception (error) in


try block code execution then after
completing try block code the else
code block will be executed.

The finally code block is executed at


the end, regardless of any exception
(error).
Classification: Internal

Look at the following code and find


out, Whether the code will RUN ?
Classification: Internal

As there is/are no error(s) in code


the program will run successfully
and produce following result.
Classification: Internal

Look at the following code and find


out, Whether the code will RUN ?
Classification: Internal

There are no syntax error in the


code. However, a runtime error will
be generated for MATH domain
error (Divide by Zero).
Classification: Internal

Look at the following code and find


out, Whether the code will RUN ?
Classification: Internal

As there is/are no error(s) in code


the program will run successfully
and produce following result.
Classification: Internal

Look at the following code and find


out, Whether the code will RUN ?
Classification: Internal

There are no syntax error in the


code. However, a runtime error will
be generated for Index domain
error (Index Out of Bounds).
Classification: Internal

Built In Exceptions
Classification: Internal

Hence, these runtime errors can be easily handled and we can ensure
the error free execution of the program. Thereby we can improve
robustness of the program using try…except block.
Classification: Internal

Hence, these runtime errors can be easily handled and we can ensure
the error free execution of the program. Thereby we can improve
robustness of the program using try…except block.
Classification: Internal

When we use multiple except block then we must assign default


except block at the end of all exact except blocks, otherwise this error
will be raised.
Classification: Internal

In python a raise statement will be used to raise a manual exception. Using raise
we can raise any built in exception.
Classification: Internal
Classification: Internal

An assert statement in Python is used to test an expression in the program code. If


the result after testing comes false, then the exception is raised.
Classification: Internal
Classification: Internal

try:
The try…except block can also be
nested. It means one try block
contains another try…except block. except:
try:

except:
In this case both try…except block
works independently. The outer one
will have its separate except, else and
finally blocks as well as inner ones else:

will also have separate except, else finally:

and finally block. else:

finally:
Classification: Internal

try: try:

except: except:

else: else:
try:

except: finally:
try:

except:

else:

finally:
else:
finally:
finally:
Classification: Internal

You might also like