Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Exception hierarchy
📖 Scenario: Imagine you are building a simple program that handles different types of errors when processing user input. You want to organize these errors in a clear way using Python's exception hierarchy.
🎯 Goal: You will create a base exception class and two specific exception classes that inherit from it. Then you will write code to raise and catch these exceptions properly.
📋 What You'll Learn
Create a base exception class called InputError that inherits from Exception.
Create two exception classes called ValueTooSmallError and ValueTooLargeError that inherit from InputError.
Write code to raise ValueTooSmallError if a number is less than 10.
Write code to raise ValueTooLargeError if a number is greater than 100.
Use a try-except block to catch these exceptions and print a message.
💡 Why This Matters
🌍 Real World
Organizing errors in a program helps keep code clean and makes it easier to find and fix problems.
💼 Career
Understanding exception hierarchy is important for writing robust software that handles errors gracefully in professional development.
Progress0 / 4 steps
1
Create the base exception class
Create a class called InputError that inherits from Exception.
Python
Hint
Use class InputError(Exception): and add pass inside.
2
Create specific exception classes
Create two classes called ValueTooSmallError and ValueTooLargeError that both inherit from InputError.
Python
Hint
Define each class with class ClassName(InputError): and add pass inside.
3
Raise exceptions based on number value
Create a variable called number and set it to 5. Then write code to raise ValueTooSmallError if number is less than 10, and raise ValueTooLargeError if number is greater than 100.
Python
Hint
Use if number < 10: and raise ValueTooSmallError("message"). Use elif number > 100: and raise ValueTooLargeError("message").
4
Catch and handle the exceptions
Use a try-except block to catch ValueTooSmallError and ValueTooLargeError. Print "Caught too small error" if ValueTooSmallError is caught, and print "Caught too large error" if ValueTooLargeError is caught.
Python
Hint
Use try: block around the code that raises exceptions. Use except ValueTooSmallError: and except ValueTooLargeError: to catch and print messages.
Practice
(1/5)
1. Which of the following is the base class for all built-in exceptions in Python?
easy
A. Exception
B. BaseException
C. Error
D. RuntimeError
Solution
Step 1: Understand Python's exception hierarchy
All exceptions in Python inherit from BaseException, which is the root of the hierarchy.
Step 2: Identify the base class
Exception inherits from BaseException, but BaseException is the top-level base class.
Final Answer:
BaseException -> Option B
Quick Check:
BaseException is the root of all exceptions [OK]
Hint: Remember: BaseException is the root of all exceptions [OK]
Common Mistakes:
Confusing Exception with BaseException
Thinking Error is a built-in base class
Choosing RuntimeError as base
2. Which of the following is the correct syntax to catch all exceptions except system-exiting ones?
easy
A. except SystemExit:
B. except BaseException:
C. except Exception:
D. except RuntimeError:
Solution
Step 1: Recall exception hierarchy for catching exceptions
Catching Exception catches most errors but excludes system-exiting exceptions like SystemExit and KeyboardInterrupt.
Step 2: Identify correct syntax
except Exception: is the standard way to catch all regular exceptions safely.
Final Answer:
except Exception: -> Option C
Quick Check:
Use Exception to catch all but system-exiting exceptions [OK]
Hint: Use except Exception to avoid catching system-exit errors [OK]
Common Mistakes:
Using except BaseException catches system exit too
Catching only RuntimeError misses many exceptions
Using except SystemExit catches only exit exceptions