Types of Exceptions
Types of Exceptions in Python
Errors in Python fall into two broad categories: Syntax Errors (detected by the parser before code execution begins) and Exceptions (anomalies detected during execution at runtime). When an exception is not intercepted, Python halts program execution and displays a traceback.
1. Syntax Errors vs. Runtime Exceptions
2. The Python Built-in Exception Hierarchy
All built-in exceptions form an inheritance tree rooted at BaseException. Understanding this hierarchy prevents bugs where catching a broad exception inadvertently intercepts system-level termination signals.
except BaseException:. Doing so catches KeyboardInterrupt and SystemExit, making it impossible to stop your program using Ctrl+C.3. Deep Dive into Common Exceptions
1. TypeError vs. ValueError
TypeError: An operation or function is applied to an object of inappropriate type.
ValueError: The type is correct, but the content / value is invalid.
2. LookupError: IndexError vs. KeyError
IndexError: Attempting to access an index outside the boundaries of a sequence (list, tuple).
KeyError: Looking up a dictionary key that does not exist.
3. AttributeError
Occurs when attempting to access a method or attribute that does not exist on that object type:
4. NameError vs. UnboundLocalError
NameError: Referencing a variable or function name that has not been defined in any local, enclosing, or global scope.
UnboundLocalError: Referencing a local variable before it has been assigned inside a function.
5. ModuleNotFoundError
A specialized subclass of ImportError raised when Python cannot resolve an import target:
4. Multi-Catch and Inheritance Polymorphism
Because exceptions are standard Python classes, catching a parent class catches all derived subclasses:
Multiple Choice Questions
1. Which base class sits at the very top of Python's exception hierarchy?
A. Exception B. RootError C. BaseException D. SystemError Answer: C Explanation: BaseException is the common base class for all built-in exceptions in Python.
2. What exception is raised by int("ninety-nine")?
A. TypeError B. ValueError C. CastError D. ParsingError Answer: B Explanation: Passing a string to int() satisfies the expected type, but the content cannot be parsed as an integer literal, raising a ValueError.
3. Both IndexError and KeyError inherit directly from which intermediate class?
A. CollectionError B. LookupError C. IndexableException D. KeyIndexError Answer: B Explanation: LookupError is the base class for exceptions raised when a key or index used on a mapping or sequence is invalid.
4. Why should developers avoid writing except BaseException: in normal application code?
A. It consumes too much CPU cache B. It intercepts user termination signals like KeyboardInterrupt (Ctrl+C) and SystemExit C. It only catches syntax errors D. Python 3 removed BaseException Answer: B Explanation: Catching BaseException intercepts KeyboardInterrupt and SystemExit, preventing normal script termination via Ctrl+C or sys.exit().
5. What exception is triggered when invoking (5).append(10)?
A. TypeError B. AttributeError C. ValueError D. MethodNotFoundError Answer: B Explanation: Integers do not have an append attribute or method, so Python raises an AttributeError.
Try-Except-Else-Finally
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Project: Bank Account Class System | Try-Except-Else-Finally |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.