Junior — Middle
How can you manually raise an exception in Python?
sobes.tech AI
Answer from AI
In Python, the raise statement is used for manually raising exceptions. It can be used to raise both standard and custom exceptions.
Example:
# Raising a standard exception
raise ValueError("Invalid value")
# Or creating and raising a custom exception
class MyError(Exception):
pass
raise MyError("My error occurred")
raise can also be used to re-raise an caught exception within an except block.