Error Handling

Common Types of Error:

SyntaxError

NameError

TypeError

IndexError

Attribute Error


Good Debugging techniques:

  • using print option

  • using python built in debugger (pdb)

  • using differeing IDE debugging tools


Exception Handling:

  • try…except

    • try — used on block of code that may raise an excpetion

    • exceptions takes a specific exception (XXXXErrror, etc) and runs this specific block

      • can use multiple exceptions

  • else and finally

    • can extend try and except blocks

    • finally → will always run regardless of error

  • exception object

    • ex except ValueError as e:

      • lets you access exception for better debung and print direcr error messages

      • use as keyword to access

  • raise statement

    • throw an exception when a certain condition is met.

    • manually raise an exception

    • ex
          def divide(a, b): if b == 0: raise ZeroDivisionError('You cannot divide by zero') return a / b


Exception Signaling:

  • can be used to create custom error excepiron

  • can also be used with the from keyword

    • chain exceprions