Skip to content

Computer Science · Ch 1 — Exception Handling in Python

Summary

Summary

  • Syntax errors (parsing errors) are detected when the rules of the programming language have not been followed while writing a program.
  • When a syntax error is encountered, Python displays the name of the error along with a short description of what went wrong.
  • A program will not start executing until every syntax error has been rectified.
  • An exception is a Python object that represents an error.
  • Syntax errors are themselves also handled as a kind of exception.
  • An exception must be handled by the programmer so that the program does not terminate abruptly.
  • When an exception occurs during execution and a built-in exception is already defined for that situation, the appropriate handler code runs and displays the reason for the error along with the exception's name. The programmer then takes the appropriate action to handle it.
  • Some of the commonly occurring built-in exceptions are SyntaxError, ValueError, IOError, KeyboardInterrupt, ImportError, EOFError, ZeroDivisionError, IndexError, NameError, IndentationError, TypeError, and OverFlowError.
  • When an error is encountered in a program, the Python interpreter raises (throws) an exception.
  • Exception handlers are blocks of code designed to execute when a specific exception is raised.
  • Raising an exception means interrupting the normal flow of program execution and jumping to the exception handler.
  • The raise and assert statements are both used to forcefully raise exceptions.
  • The process of exception handling involves writing additional code that gives the user proper messages or instructions, so that the program does not crash abruptly. This additional code is what is meant by an exception handler. …