Exception Handling - Study Notes
Exception Handling - Study Notes
Study Notes
Introduction
Exceptions are errors that occur during a Python program's execution, leading to unexpected
output or abnormal behavior
They arise from syntax errors, runtime errors, or logical errors in the code
Python exceptions are automatically triggered but can also be forcefully triggered and handled
via code
Syntax Errors
Syntax errors, also known as parsing errors, are detected when the rules of the programming
language are violated
The interpreter does not execute the program until syntax errors are corrected
Python displays the error name and a description when a syntax error occurs in shell mode
When running a program in script mode, a dialog box appears with the error details
Exceptions
Exceptions can occur even if the syntax is correct; for example, trying to open a non-existent file
or dividing by zero
An exception is a Python object that represents an error, and an exception is "raised" when an
error occurs
Programmers should anticipate potential errors and include code to handle them
Built-in Exceptions
Python's standard library offers built-in exceptions for common errors, providing standardized
solutions
When a built-in exception occurs, the corresponding exception handler code executes,
displaying the reason and exception name
ValueError: Raised when a method or operation receives an argument with the correct data
type but an inappropriate value
KeyboardInterrupt: Raised when the user interrupts program execution with Delete or Esc
EOFError: Raised when the end of a file is reached without reading data via input()
OverflowError: Raised when a calculation exceeds the maximum limit for a numeric data type
Raising Exceptions
Programmers can forcefully raise exceptions using the raise and assert statements
Raising an exception interrupts the normal program flow, transferring control to the exception
handler code
Primarily used at the beginning of a function or after a function call to validate input
Handling Exceptions
Exception handlers separate main logic from error detection and correction code
When an error occurs, the interpreter creates an exception object with details like type, file
name, and position
This object is handed to the runtime system to find suitable handling code; this is "throwing" an
exception
The runtime system searches for an exception handler, checking the current method and calling
methods recursively (call stack)
Catching Exceptions
Suspicious code lines are placed in a try block, followed by an except block containing code to
handle possible exceptions
try...except Block
try:
except [exception-name]:
# code for handling the exception-name error
If an exception occurs, the try block stops, and control transfers to the except block
Multiple except blocks can handle different types of errors for a single try block
An except clause without a specified exception can handle any unhandled exception
try...except...else Clause
If no exception is raised in the try block, the statements in the else clause are executed
finally Clause
Statements inside the finally block always execute, regardless of whether an exception occurred
If used, finally should be placed at the end of the try clause, after all except blocks and
the else block
If an error is detected and an exception is thrown, the appropriate except block handles the
error
After the finally block, Python transfers control to a previously entered try block or a higher-
level exception handler