Exception Handling - Python

  • Try
        try:
            # Statements
        except:
            print(e)
    
  • Catching Exception
        try:
            # Statements
        except Exception as e:
            print(e)
    
  • Catching Specific Exception
        try:
            # Statements
        except ErrorName1:
            print(e)
        except ErrorName2:
            print(e)
    
  • Finally
        try:
            # Statements
        except:
            # Statements
        finally:
            # Statements
    
  • Error Objects
    • ValueError
    • IndexError
  • Raise Custom Error
        if condition:
            raise ErrorName("value")
    
  • Custom Exceptions
        class CustomException(Exception):
            # Statements
            try:
                # Statements
            except CustomError:
                # Statements
    
Share: