The raise
statement in Python is used to explicitly trigger an exception. It can be used to raise built-in exceptions or custom exceptions. Here’s how you can use it:
Raising a Built-in Exception
You can raise an exception by specifying the exception type.
1
| raise ValueError("Invalid input!")
|
1
2
3
4
5
6
| ---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[7], line 1
----> 1 raise ValueError("Invalid input!")
ValueError: Invalid input!
|
Raising a Custom Exception
You can define your own exception class by inheriting from Exception
and then raise it.
1
2
3
4
| class CustomError(Exception):
pass
raise CustomError("This is a custom exception.")
|
1
2
3
4
5
6
7
8
| ---------------------------------------------------------------------------
CustomError Traceback (most recent call last)
Cell In[8], line 4
1 class CustomError(Exception):
2 pass
----> 4 raise CustomError("This is a custom exception.")
CustomError: This is a custom exception.
|
Raising an Exception Conditionally
You can define your own exception class by inheriting from Exception
and then raise it.
1
2
3
4
5
6
| def divide(a, b):
if b == 0:
raise ZeroDivisionError("Division by zero is not allowed.")
return a / b
divide(5, 0) # This will raise an exception
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| ---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
Cell In[9], line 6
3 raise ZeroDivisionError("Division by zero is not allowed.")
4 return a / b
----> 6 divide(5, 0) # This will raise an exception
Cell In[9], line 3, in divide(a, b)
1 def divide(a, b):
2 if b == 0:
----> 3 raise ZeroDivisionError("Division by zero is not allowed.")
4 return a / b
ZeroDivisionError: Division by zero is not allowed.
|
Re-raising an Exception
Inside an except
block, you can use raise
to propagate an exception.
x = int("abc")
triggers an exception.
1
2
3
4
5
6
| ---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[21], line 1
----> 1 x = int("abc")
ValueError: invalid literal for int() with base 10: 'abc'
|
x = int("abc")
triggers an exception. The exception is ValueError
, so you can use ValueError
instead of Exception
.
1
2
3
4
| try:
x = int("abc") # This will cause a ValueError
except Exception as e:
raise e
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| ---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[18], line 4
2 x = int("abc") # This will cause a ValueError
3 except Exception as e:
----> 4 raise e
Cell In[18], line 2
1 try:
----> 2 x = int("abc") # This will cause a ValueError
3 except Exception as e:
4 raise e
ValueError: invalid literal for int() with base 10: 'abc'
|
1
2
3
4
| try:
x = int("abc") # This will cause a ValueError
except ValueError as e:
raise e
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| ---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[22], line 4
2 x = int("abc") # This will cause a ValueError
3 except ValueError as e:
----> 4 raise e
Cell In[22], line 2
1 try:
----> 2 x = int("abc") # This will cause a ValueError
3 except ValueError as e:
4 raise e
ValueError: invalid literal for int() with base 10: 'abc'
|
x = int("abc")
triggers an exception. The exception is ValueError
, so you can use ValueError
instead of Exception
. If you use ZeroDivisionError
, the program will not enter the except
block when the exception is triggered.
1
2
3
4
| try:
x = int("abc") # This will cause a ValueError
except ZeroDivisionError as e:
raise e
|
1
2
3
4
5
6
7
8
9
| ---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[19], line 2
1 try:
----> 2 x = int("abc") # This will cause a ValueError
3 except ZeroDivisionError as e:
4 raise e
ValueError: invalid literal for int() with base 10: 'abc'
|