Constant dripping wears away a stone


Exception Handling: Exceptions are not the same as syntax errors. A statement could cause an exception even if its syntax is correct. Most exceptions are not handled by Python programs and result in error messages. For example, print('Hello'will raise a syntax error whilea = 5/0will cause the exceptionZeroDivisionerror.

  • Built-in Exceptions: Built-in exceptions in Python are generated by the interpreter or built-in functions of Python. The exceptions print a detailed information about the causes of the exceptions. The class hierarchy for built-in exceptions is shown as the following picture taken from this webpage
  • User-defined Exceptions: Users can create their own exception classes based on theExceptionclass.
  • Handling Exceptions: Users can write programs to handle exceptions usingtrystatements. Eachtrystatement consists of atryblock and anexceptblock. Thetryblock includes a block of statements written to test for errors and theexceptblock includes a block of statements written to handle the error.
  • Raising Exceptions: Theraisestatement forces a specified exception to occur.
  • Exception Chaining: Exception chaining happens when an exception is raised inside anexceptor afinallyblock. The raisestatement has an optionalfromclause that enables chaining exceptions. Exception chaining will be disabled if putfrom None in theraisestatement.
  • Defining Clean-up Actions: Atryblock can have an optionalfinallyblock to define clean-up actions that must be executed no matter whether the try block causes an exception or not.