Question
What is the use of raise in Python?
Solution
✔ Verified
The raise keyword in Python is used to manually trigger an exception (error) in your program.
Why use raise?
- To show a custom error message.
- To stop the program when something goes wrong.
- To make sure the program follows certain rules.
Example:--
age = -5
if age < 0:
raise ValueError("Age cannot be negative")
Click here to download practice questions on
Exception Handling in PythonMore Questions on Exception Handling in Python
Question 6
"Every syntax error is an exception but every exception cannot be a syntax error." Justify the statement.
View solution