Project 21: Music Player UI
Python Challenge
Builds on these lessons
Step 1 of 3
Project
A Custom Exception
Define class TrackNotFoundError(Exception): and raise it from find_track when nothing matches. A custom exception type lets calling code catch exactly this failure specifically — except TrackNotFoundError: — instead of a generic except Exception: that would also swallow unrelated bugs.
🎯 Your Task
Please add the exact code shown in the light gray box below to your editor.Do not delete your previous code, just insert these new lines in the correct place!
class TrackNotFoundError(Exception):
pass
tracks = {"Horizon": "Aurora Fields"}
def find_track(title):
if title not in tracks:
raise TrackNotFoundError(f"No track named '{title}'")
return tracks[title]
try:
find_track("Nonexistent Song")
except TrackNotFoundError as e:
print(e)