KEMBAR78
Python_Exception_Handling_Presentation.pptx
Exception Handling in Python
What is an Exception?
Exceptions are noting but the errors detected during execution of the
program. They interrupt the normal flow of a program’s instructions.
What is Exception handling ?
The machine expects that something might go wrong and knows how to
respond without crashing. The program creates an “exception” object
that contains information about the error. The process of responding to
this exception is called “exception handling”.
Why Exception Handling?
• - To prevent program crashes.
• - To handle errors gracefully.
• - To improve debugging and error tracing.
• - To ensure the program continues or fails safely.
Process of exception handling
Basic Try-Except Block
try:
# Code that may raise an exception
result = 10 / 0
except ZeroDivisionError:
# Handling specific exception
print("Cannot divide by zero!")
Handling Multiple Exceptions
Handling multiple exceptions means being ready for different kinds of errors,
and knowing exactly how to respond to each — just like solving different
problems in everyday life.
Else Clause:
else`: Runs if no exceptions occur.
Finally Clause:
`finally`: Always executes, whether exception occurred or not.
Catch-All Exception Handling:
You can use a general except block to catch any exception that wasn't specifically
handled.
raise Keyword
The raise keyword in Python is used to manually trigger an exception. It tells the
program: “Something went wrong — stop and handle this issue.”
custom exception
A custom exception is a user-defined error type that you create to handle
specific problems in your program more clearly and meaningfully.
Raising Exceptions
• def bake_cake(eggs):
• if eggs == 0:
• # Raise an exception if no eggs
• raise ValueError("You need at least one egg to bake a cake!")
• print("Cake is baking...")
• # Now let's handle the exception when calling the function
• try:
• bake_cake(0) # Trying to bake with 0 eggs
• except ValueError as e:
• print("Error:", e)
Understanding Exceptions
BaseException
The root class for all exceptions in
Python.
Popular Built-ins
• TypeError
• NameError
• IndexError
• IOError
Common Exceptions
•ArithmeticError
•LookupError
•ValueError
Common Python Exceptions
• - ArithmeticError
• - AssertionError
• - AttributeError
• - EOFError
• - ImportError
• - IndexError
• - KeyError
• - KeyboardInterrupt
• - MemoryError
• - NameError
• - NotImplementedError
• - OSError
• - OverflowError
• - RecursionError
• - RuntimeError
• - StopIteration
• - SyntaxError
• - IndentationError
• - TabError
• - SystemError
• - TypeError
• - UnboundLocalError
• - ValueError
• - ZeroDivisionError
Custom Exceptions
• ```python
• class CustomError(Exception):
• pass
• raise CustomError("A custom error occurred")
• ```
• - Define your own exception types.
Best Practices
• - Catch specific exceptions.
• - Avoid bare excepts (except:)
• - Clean up resources (use finally or with)
• - Log errors for debugging.
Thank You!
• Questions?

Python_Exception_Handling_Presentation.pptx

  • 1.
  • 2.
    What is anException? Exceptions are noting but the errors detected during execution of the program. They interrupt the normal flow of a program’s instructions. What is Exception handling ? The machine expects that something might go wrong and knows how to respond without crashing. The program creates an “exception” object that contains information about the error. The process of responding to this exception is called “exception handling”.
  • 3.
    Why Exception Handling? •- To prevent program crashes. • - To handle errors gracefully. • - To improve debugging and error tracing. • - To ensure the program continues or fails safely.
  • 4.
  • 5.
    Basic Try-Except Block try: #Code that may raise an exception result = 10 / 0 except ZeroDivisionError: # Handling specific exception print("Cannot divide by zero!")
  • 6.
    Handling Multiple Exceptions Handlingmultiple exceptions means being ready for different kinds of errors, and knowing exactly how to respond to each — just like solving different problems in everyday life.
  • 8.
    Else Clause: else`: Runsif no exceptions occur. Finally Clause: `finally`: Always executes, whether exception occurred or not. Catch-All Exception Handling: You can use a general except block to catch any exception that wasn't specifically handled. raise Keyword The raise keyword in Python is used to manually trigger an exception. It tells the program: “Something went wrong — stop and handle this issue.” custom exception A custom exception is a user-defined error type that you create to handle specific problems in your program more clearly and meaningfully.
  • 9.
    Raising Exceptions • defbake_cake(eggs): • if eggs == 0: • # Raise an exception if no eggs • raise ValueError("You need at least one egg to bake a cake!") • print("Cake is baking...") • # Now let's handle the exception when calling the function • try: • bake_cake(0) # Trying to bake with 0 eggs • except ValueError as e: • print("Error:", e)
  • 10.
    Understanding Exceptions BaseException The rootclass for all exceptions in Python. Popular Built-ins • TypeError • NameError • IndexError • IOError Common Exceptions •ArithmeticError •LookupError •ValueError
  • 11.
    Common Python Exceptions •- ArithmeticError • - AssertionError • - AttributeError • - EOFError • - ImportError • - IndexError • - KeyError • - KeyboardInterrupt • - MemoryError • - NameError • - NotImplementedError • - OSError • - OverflowError • - RecursionError • - RuntimeError • - StopIteration • - SyntaxError • - IndentationError • - TabError • - SystemError • - TypeError • - UnboundLocalError • - ValueError • - ZeroDivisionError
  • 12.
    Custom Exceptions • ```python •class CustomError(Exception): • pass • raise CustomError("A custom error occurred") • ``` • - Define your own exception types.
  • 13.
    Best Practices • -Catch specific exceptions. • - Avoid bare excepts (except:) • - Clean up resources (use finally or with) • - Log errors for debugging.
  • 14.