Skip to content

Latest commit

 

History

History
81 lines (78 loc) · 3.52 KB

04. error handling.md

File metadata and controls

81 lines (78 loc) · 3.52 KB

Error Handling

  • a crucial aspect of writing robust and reliable programs
  • involves identifying, handling, and potentially recovering from errors or exceptions that may occur during the execution of a Python program
  • Errors can occur for various reasons, such as incorrect input, file not found, division by zero, and many others. Python provides several mechanisms for error handling
  1. exceptions
    • in Python, errors are represented as exceptions
    • when an error occurs, an exception object is raised, and the program stops executing unless the exception is caught and handled
  2. Try-Except Blocks
    • the most common way to handle exceptions is by using try-except blocks
    • enclose the code that might raise an exception within a try block, and you specify the handling code in an except block
    try:
        # Code that may raise an exception
    except SomeException:
        # Code to handle the exception
    • example:
      try:
          result = 10 / 0 # This will raise a ZeroDivisionError
      except ZeroDivisionError as e:
          pritn(f"Error: {e}")
  3. Multiple Except Blocks
    • can use multiple except blocks to handle different types of exceptions
    • python allows you to catch and handle different exceptions in separate blocks
    try:
        # Code that may raise an exception
    except FirstException:
        # Code to handle the first exception
    except SecondException:
        # Code to handle the second exception
  4. Else Clause
    • can use an else clause in a try-except block to specify code that should be executed if no exceptions are raised
    try:
        # Code that may raise an exception
    except SomeException:
        # Code to handle the exception
    else:
        # Code to execute if no exception is raised
  5. Finally Block
    • the finally block is used to specify code that should be executed regardless of whether an exception was raised or not
    • it is commonly used for cleanup operations
    try:
        # Code that may raise an exception
    except SomeException:
        # Code to handlethe exception
    finally:
        # Code that always runs, even is an exception was raised
  6. Custom Exceptions
    • can create your own custom exception classes by inheriting from the Exception class or one of its subclasses
    • allows to define specific exceptions tailored to your application
    class CustomError(Exception):
        pass
  7. Raising Exceptions
    • can raise exceptions explicitly using the raise statement, allowing you to signal errors or exceptional conditions in your code
    if condition:
        raise CustomError("This is a custom error message")
  8. Exception Hierarchy
    • Python has a built-in exception hierarchy with a base class BaseException at the top
    • more specific exception classes are derived from it
    • This hierarchy allows to catch exceptions at different levels of granularity
      • granularity: 세분성. 무언가를 더 작은 단위로 표현하는 것을 말함
  9. Common Exception Types
    • Python has several built-in exception types, including ZeroDivisionError, ValueError, FileNotFoundError, and TypeError, among others
    • Understanding these exception types can help you handle errors effectively.
  10. Logging
    • can use the Python logging module to log error messages and other information to a log file or the console, helping with debugging and monitoring