raise is used to manually throw an exception at the specified position of the program
The basic syntax format of raise statement is:
raise [exceptionName [(reason)]]
Among them, the parameters enclosed by [] are optional parameters, which are used to specify the name of the thrown exception and the relevant description of the exception information. If all optional parameters are omitted, raise will throw the current error as it is; If only reason is omitted, no exception description information will be attached when throwing an exception.
In other words, raise statements have the following three common uses:
1. Raise: a separate raise. This statement throws an exception caught in the current context (such as in the except block), or throws a RuntimeError exception by default.
2. Raise exception class name: raise is followed by an exception class name, indicating that an exception of execution type is thrown.
3. raise exception class name (description information): when an exception of the specified type is thrown, the description information of the exception is attached.
Obviously, each time the raise statement is executed, an exception can only be raised once. First, let's test the usage of the above three raise:
fish@asus:~$ python3 Python 3.6.9 (default, Mar 15 2022, 13:55:28) [GCC 8.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> raise Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: No active exception to reraise >>> raise ZeroDivisionError Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError >>> raise ZeroDivisionError("Divisor cannot be zero") Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: Divisor cannot be zero >>>
Of course, we manually make the program throw exceptions, and most of the time we don't want it to crash. In fact, the exception thrown by raise statement is usually caught and handled by try except (else finally) exception handling structure. For example:
try: a = input("Enter a number:") #Judge whether the number entered by the user is a number if(not a.isdigit()): raise ValueError("a Must be a number") except ValueError as e: print("Exception thrown:",repr(e))
The running result of the program is:
Enter a number: a Exception thrown: ValueError('a Must be a number',)
It can be seen that when the user does not enter a number, the program will enter the if judgment statement and execute raise, causing ValueError exception. However, because it is in the try block, the exception thrown by raise will be caught by try and handled by the exception block.
Therefore, although the raise statement is used in the program to throw exceptions, the execution of the program is normal, and the exceptions thrown manually will not cause the program to crash.
raise does not require parameters
As you can see earlier, the raise statement can be used without parameters, for example:
try: a = input("Enter a number:") if(not a.isdigit()): raise ValueError("a Must be a number") except ValueError as e: print("Exception thrown:",repr(e)) raise
The program execution result is:
Enter a number: a Exception thrown: ValueError('a Must be a number',) Traceback (most recent call last): File "D:\python3.6\1.py", line 4, in <module> raise ValueError("a Must be a number") ValueError: a Must be a number
Here we focus on the raise in the except block. Since we have manually raised the ValueError exception before it, it will be raised again when we use the raise statement again.
When a raise statement without parameters is used in a program that has not thrown an exception, it throws a RuntimeError exception by default. For example:
try: a = input("Enter a number:") if(not a.isdigit()): raise except RuntimeError as e: print("Exception thrown:",repr(e))
The program execution result is:
Enter a number: a Exception thrown: RuntimeError('No active exception to reraise',)