raise Exception in Python
Learn how to manually raise exceptions in Python using
the raise statement.
What is raise?
The raise statement is used to manually
generate an exception in Python.
We can use raise when a program detects
an invalid situation and wants to stop the current
operation.
raise statement is used to explicitly
generate an exception.
raise Flow
Program Condition
Python checks whether a condition is valid.
raise Exception
The program manually generates an exception.
except Block
The exception can be handled using except.
Basic Syntax
raise Exception("Error message")
The raise keyword is followed by an
exception object.
Simple Example
age = 15
if age < 18:
raise Exception("Age must be 18 or above")
Output:
Exception: Age must be 18 or above
The program manually raises an exception because the age is less than 18.
Raising ValueError
We can raise a specific exception such as
ValueError.
marks = -10
if marks < 0:
raise ValueError("Marks cannot be negative")
Output:
ValueError: Marks cannot be negative
raise with try and except
A manually raised exception can be handled using
try and except.
try:
age = 15
if age < 18:
raise ValueError("Age must be 18 or above")
except ValueError as error:
print(error)
Output:
Age must be 18 or above
raise with User Input
We can use raise to validate user input.
try:
age = int(
input("Enter your age: ")
)
if age < 18:
raise ValueError("You must be 18 or above")
print("You are eligible")
except ValueError as error:
print("Error:", error)
If the user enters an age below 18, the program
manually raises ValueError.
Example: Validate Student Marks
try:
marks = int(
input("Enter marks: ")
)
if marks < 0 or marks > 100:
raise ValueError("Marks must be between 0 and 100")
print("Valid marks:", marks)
except ValueError as error:
print("Error:", error)
Example Output:
Enter marks: 120
Error: Marks must be between 0 and 100
Raising Different Exception Types
Python allows us to raise different types of exceptions depending on the situation.
number = -5
if number < 0:
raise ValueError("Number cannot be negative")
Here, ValueError is raised because
the supplied value is invalid.
raise Without a Message
An exception can also be raised without providing a custom message.
age = 15
if age < 18:
raise ValueError
This raises a ValueError without
a custom error message.
Raising an Existing Exception
An exception can be caught and raised again using
the raise statement.
try:
number = int("abc")
except ValueError:
print("Handling error")
raise
The raise statement raises the same
exception again.
Practical Uses of raise
Validation
Used to reject invalid input values.
Business Rules
Used when application rules are not satisfied.
Error Handling
Used to manually generate meaningful errors.
Best Practices
Clear Messages
Provide a clear message explaining the problem.
Correct Exception
Choose an exception type that matches the actual problem.
Validate Early
Validate important data before continuing with further operations.
Common Mistakes
- Raising an exception without a meaningful reason.
- Using the wrong exception type.
- Raising exceptions for normal program flow.
- Providing unclear or misleading error messages.
Practice Programs
-
Raise a
ValueErrorif age is below 18. - Raise an exception when marks are outside the range 0 to 100.
- Create a calculator that raises an exception when division by zero is attempted.
- Take user input and manually raise an exception for invalid input.
-
Catch an exception and raise it again using
the
raisestatement.
Summary
The raise statement is used to manually
generate an exception in Python. It is useful for
validation, business rules and error handling.
We can raise built-in exceptions such as
ValueError and handle them using
try and except.