Question

What is the use of raise in Python?

Updated on May 31, 2025 | By Learnzy Admin | 👁️ Views: 146 students

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")
Was this solution helpful? 28
Click here to download practice questions on Exception Handling in Python

More Questions on Exception Handling in Python

Question 1

What is the purpose of the else block in exception handling?


View solution
Question 2

How can you catch multiple exceptions in one block?


View solution
Question 3

What is the use of the finally block?


View solution
Question 4

Write the syntax of exception handling in Python.


View solution
Question 5

Name any four common exceptions in Python.


View solution
Question 6

"Every syntax error is an exception but every exception cannot be a syntax error." Justify the statement.


View solution