Errors

There are two main categories of errors, Exceptions and Syntax Errors.

Syntax errors occur when something goes wrong while Python is parsing/reading/compiling the code (`.py` to `.pyc`), meaning the proper syntax of the language is not followed.

Logical errors or Exceptions are raised during runtime, when some internal events occur which changes the normal flow of the program.

So a syntax error in Python occurs during the process of parsing the code, which happens before the code is executed. This is how Python executes scripts:

1. Tokenization

The first step when running a Python script is lexing or tokenization. The Python interpreter reads the `.py` file and breaks down the source code into meaningful elements called tokens, for example keywords, variable names and operators.

2. Parsing

The next step after tokenization is parsing, meaning Python's parser analyzes the structure of the tokens according to the rules of Python syntax. It builds an Abstract Syntax Tree (AST) to represent the program's structure.

If there is any issue with the syntax (e.g., mismatched parentheses, missing colons, invalid indentation), a syntax error will be raised at this stage. Since this happens before execution, no actual code runs if a syntax error is detected.

3. Bytecode Compilation

If the code has no syntax errors, the Python interpreter compiles the parsed code (AST) into bytecode. This bytecode is a lower-level, platform-independent representation of the Python code, which is stored in `.pyc` files (compiled Python files).

The `.pyc` files are created to speed up subsequent executions, as Python can skip the parsing and compilation steps and directly interpret the bytecode.

4. Execution in the Python Virtual Machine (PVM)

Once the bytecode is generated, the Python Virtual Machine (PVM) executes it. The PVM is the runtime engine that reads and executes the bytecode instructions.

Since syntax errors are detected during parsing, before the code is compiled into bytecode, they prevent the creation of the `.pyc` file and the execution of the code by the PVM. So syntax errors must be fixed before the script can run, as they prevent Python from reaching the execution phase.

Exceptions

Exceptions occur when an error is detected during program execution, rather than during the parsing of the code. They indicate that something unexpected or invalid has happened, such as trying to divide by zero, accessing a variable that doesn't exist, or trying to open a file that isn't found.

  • Runtime Errors: Exceptions happen while the program is running
  • Interrupt Execution: If not handled, exceptions stop the execution of the program and display a traceback.
  • Type Hierarchy: Exceptions are part of a built-in class hierarchy in Python, with the base class being `BaseException`.
# ZeroDivisionError print(10 / 0) # Causes a division by zero error # FileNotFoundError with open('nonexistent_file.txt') as f: data = f.read() # NameError print(undefined_variable) # Variable does not exist

Python provides a mechanism to handle exceptions using try, except, else and finally blocks to prevent crashes and allow the program to recover gracefully.

try: result = 10 / 0 except ZeroDivisionError as e: print("You can't divide by zero!") else: print("Division successful!") finally: print("Execution complete.")

Logical or semantic errrors occur when the program runs without crashing but produces incorrect or unintended results. These errors arise due to a mistake in the program's logic or algorithm. Logical errors are difficult to detect since they do not raise exceptions and the code executes as if everything is fine.