Scrooge McDuck is diving into his money bin, and everything is going great… until something goes wrong. Maybe the vault code is entered incorrectly. Maybe someone tries to withdraw more money than actually exists. Or maybe a Beagle Boy sneaks in and breaks the rules. In real life, Scrooge doesn’t let the entire money bin explode just because of one mistake. Instead, he stops the problem, sounds the alarm, and handles it safely.
Python works the same way. When something goes wrong in a program, we don’t always want it to crash and burn. Sometimes we want to intentionally stop the program and explain what went wrong. That’s where exceptions come in. Python can throw an error on purpose using raise, catch that error safely using except … as e, and then continue running without everything falling apart.
Today, you’re going to learn how to control errors instead of being controlled by them. You’ll see how Python can send clear messages when rules are broken, how we can catch those messages, and how we can respond instead of crashing. The big idea to remember is this: errors aren’t failures — they’re messages.
What Are Exceptions?
Errors that can crash a program
Why they Matter?
To Keep Programs running smoothly
How to Handle Them?
Try/Except Blocks
When a program runs into a problem, Python doesn’t always have to crash. Sometimes, the programmer chooses to stop the program because a rule was broken. This is called raising an exception.
raise Exception("Vault access denied")
Python is doing something very specific. It is intentionally creating a problem and stopping the normal flow of the program. This is not an accident or a bug — it is Python saying, “Something is wrong, and I need you to deal with it.”
Think of raise as pulling a fire alarm. Nothing is on fire, but you are forcing everyone to stop and pay attention.
The text inside the parentheses is the message. It explains why the alarm was triggered.
The try block says: “Run this code, but be ready in case something goes wrong.”
When the error is raised, Python immediately jumps to the except block.
Exception tells Python what kind of problem we are looking for.
as e means: “Save the error in a variable named e so we can look at it.”
The variable e now contains the message that was raised earlier. That’s why print(e) prints "Vault access denied".
So instead of crashing, the program catches the problem, reads the message, and keeps going.
raise Exception as e
Why? Because e does not exist yet.
You can only name the error after you catch it, not when you throw it. When you raise an exception, you are creating the problem. When you catch it, you are allowed to give it a name and inspect it.
A good way to remember this:
You don’t label the note while you’re throwing it
you label it after you catch it.
So far, we’ve seen that Python can throw one error when something goes wrong. But in real programs, different things can go wrong for different reasons. Python lets us throw multiple exceptions, each with its own message, so we know exactly what happened.
The key idea is this:
One program can raise many different errors — Python stops at the first one it hits.
Here's an example...
Remember, we're not yet creating custom exceptions. We'd need to learn about classes to do that. But, for now, we can give ourselves different "escapes"
Scrooge McDuck is testing the security systems in his money bin. Before anyone is allowed inside, they must pass a series of rule checks. If any rule is broken, the system immediately throws an error and explains what went wrong — without crashing the program.
Your job is to build the Vault Security Checker.
You will write a function that decides whether someone is allowed to enter the vault. The function must check multiple rules and throw an error as soon as one rule is broken. The program must then catch the error, print a clear message, and continue running safely.
Your program must check all of the following rules in order:
The vault code must be exactly 313.
If it is not, the program must raise an exception with a message explaining that the vault code is incorrect.
The character trying to enter must not be "Beagle Boy".
If the character is a Beagle Boy, the program must raise an exception explaining that intruders are not allowed.
The security level must be between 1 and 5 (inclusive).
If it is outside this range, the program must raise an exception explaining that the security level is invalid.
Only one exception should be raised per run. As soon as one rule fails, the function should stop checking the remaining rules.
Successful Run
Code Fail
Beagle Boy Fail
Security Level Fail (Too Low)