Python Tutorial
Error Handling and Exception Handling in
Python:
Key Concepts
• Error handling is the process of recognizing and dealing with
errors during the execution of a program.
• Exception handling is a specific type of error handling that
allows a program to continue running even when an error
occurs.
• In Python, exceptions are errors that occur during the execution
of a program.
• When an exception occurs, the normal flow of the program is
interrupted and the interpreter looks for an exception handler to
handle the exception.
• If an exception handler is not found, the program will crash.
Types of Exceptions
• There are many different types of exceptions in Python,
including:
• SyntaxError: Raised when there is a syntax error in the
code.
• NameError: Raised when a variable is not defined.
• TypeError: Raised when an operation or function is applied
to an object of inappropriate type.
• ValueError: Raised when an operation or function receives
an argument of correct type but inappropriate value.
, • ZeroDivisionError: Raised when the second argument of a
division or modulo operation is zero.
• FileNotFoundError: Raised when a file or directory is
requested but doesn't exist.
Handling Exceptions
• To handle exceptions, you can use a try/except block. The code
that might raise an exception is placed in the try block, and the
code that handles the exception is placed in the except block.
• Here is an example:
try:
# code that might raise an exception
x=1/0
except ZeroDivisionError:
# code to handle the exception
print("Cannot divide by zero.")
• You can also use the finally block to specify code that should be
executed whether an exception is raised or not.
try:
# code that might raise an exception
x=1/0
except ZeroDivisionError:
# code to handle the exception
print("Cannot divide by zero.")
finally:
# code that will always run
Error Handling and Exception Handling in
Python:
Key Concepts
• Error handling is the process of recognizing and dealing with
errors during the execution of a program.
• Exception handling is a specific type of error handling that
allows a program to continue running even when an error
occurs.
• In Python, exceptions are errors that occur during the execution
of a program.
• When an exception occurs, the normal flow of the program is
interrupted and the interpreter looks for an exception handler to
handle the exception.
• If an exception handler is not found, the program will crash.
Types of Exceptions
• There are many different types of exceptions in Python,
including:
• SyntaxError: Raised when there is a syntax error in the
code.
• NameError: Raised when a variable is not defined.
• TypeError: Raised when an operation or function is applied
to an object of inappropriate type.
• ValueError: Raised when an operation or function receives
an argument of correct type but inappropriate value.
, • ZeroDivisionError: Raised when the second argument of a
division or modulo operation is zero.
• FileNotFoundError: Raised when a file or directory is
requested but doesn't exist.
Handling Exceptions
• To handle exceptions, you can use a try/except block. The code
that might raise an exception is placed in the try block, and the
code that handles the exception is placed in the except block.
• Here is an example:
try:
# code that might raise an exception
x=1/0
except ZeroDivisionError:
# code to handle the exception
print("Cannot divide by zero.")
• You can also use the finally block to specify code that should be
executed whether an exception is raised or not.
try:
# code that might raise an exception
x=1/0
except ZeroDivisionError:
# code to handle the exception
print("Cannot divide by zero.")
finally:
# code that will always run