Exception Handling
in Python
What is an Exception?
In Python, an exception is an event
that occurs during the execution of a
program that disrupts the normal
flow of the program's instructions.
Why exceptions occur in
programs?
Code errors: A software engineer may make a mistake, such as
using a null resource or passing the wrong parameter to a function
Invalid user input: The user may input something that is invalid
or unacceptable
Device failure: A system failure can occur, which is a run-time
error that cannot be handled programmatically
Arithmetic errors: A program may make a mistake in a
mathematical calculation or operation
Importance of Exception
Handling
Exception handling is important because it allows programs to handle errors that occur
during execution. It's especially important in the following situations:
•Handling errors
Exception handling allows programs to distinguish between normal and erroneous return values. This is
especially important in geospatial programming, where issues like missing files or corrupt data can occur.
Separating error-handling code
Exception handling separates the details of what to do when something unexpected happens from the main logic of a
program
Handling concurrency
In multi-threaded applications, exceptions thrown by one thread can propagate to other threads, causing the application to
crash. Exception handling can prevent this from happening.
Common Python Exceptions
The TRY block lets you test a block of code for
error.
The EXCEPT block lets you handle the error.
Syntax:
try: # Code that may cause an exception except
ExceptionType: # Code to handle the exception
The try-except Block
try:
x = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
The try-except Block
try:
even_numbers = [2,4,6,8]
print(even_numbers[5])
except ZeroDivisionError:
print("Denominator cannot be 0.")
except IndexError:
print("Index Out of Bound.")
# Output: Index Out of Bound
Multiple except
Block
try:
num = int(input("Enter a
number: ")) num % 2 == 0
except:
print("Not an even number!")
else:
print("its a even number")
The else Clause

Exception Handling in Python topic .pptx

  • 1.
  • 2.
    What is anException? In Python, an exception is an event that occurs during the execution of a program that disrupts the normal flow of the program's instructions. Why exceptions occur in programs? Code errors: A software engineer may make a mistake, such as using a null resource or passing the wrong parameter to a function Invalid user input: The user may input something that is invalid or unacceptable Device failure: A system failure can occur, which is a run-time error that cannot be handled programmatically Arithmetic errors: A program may make a mistake in a mathematical calculation or operation
  • 3.
    Importance of Exception Handling Exceptionhandling is important because it allows programs to handle errors that occur during execution. It's especially important in the following situations: •Handling errors Exception handling allows programs to distinguish between normal and erroneous return values. This is especially important in geospatial programming, where issues like missing files or corrupt data can occur. Separating error-handling code Exception handling separates the details of what to do when something unexpected happens from the main logic of a program Handling concurrency In multi-threaded applications, exceptions thrown by one thread can propagate to other threads, causing the application to crash. Exception handling can prevent this from happening.
  • 4.
  • 5.
    The TRY blocklets you test a block of code for error. The EXCEPT block lets you handle the error. Syntax: try: # Code that may cause an exception except ExceptionType: # Code to handle the exception The try-except Block try: x = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero!") The try-except Block
  • 6.
    try: even_numbers = [2,4,6,8] print(even_numbers[5]) exceptZeroDivisionError: print("Denominator cannot be 0.") except IndexError: print("Index Out of Bound.") # Output: Index Out of Bound Multiple except Block try: num = int(input("Enter a number: ")) num % 2 == 0 except: print("Not an even number!") else: print("its a even number") The else Clause