1. Introduction
Python provides two very important features to handle the exceptions and errors that Python programs run in. You can use this feature to debug a python program.
- Exception handling:
- Assertion (assertions)
2. Exception handling
You can use the Try/except statement to catch an exception.
The try/except statement is used to detect errors in a try statement block, allowing the except statement to catch exception information and handle it.
If you do not want to end your program when an exception occurs, simply capture it in a try. 、
Grammar:
The following is a simple syntax for try....except...else :
3. Process
- If an exception occurs when the statement after the try is executed, Python jumps back to the try and executes the first except clause that matches the exception, and the control flow passes through the entire try statement (unless a new exception is thrown when the exception is handled).
- If an exception occurs in the statement after the try, but there is no matching except clause, the exception will be submitted to the upper try, or to the top of the program (This will end the program and print the default error message).
- If no exception occurs when the TRY clause executes, Python executes the statement after the Else statement (if there is else), and then the control flow passes through the entire try statement.
4. Use except without any exception type
The try-except statement above captures all occurrences of the exception. But this is not a good way to identify specific exception information through the program. Because it catches all the exceptions.
5. Use except with multiple exception types
6.try-finally statements
The try-finally statement executes the final code regardless of whether an exception occurs.
7. Parameters of the exception
An exception can take a parameter that can be used as the output exception information parameter.
The exception value that the variable receives is usually contained in the exception's statement.
In a tuple's form, a variable can receive one or more values.
Tuples typically contain error strings, error numbers, and error locations.
8. Triggering an exception
We can use the Raise statement to trigger an exception ourselves.
Syntax:raise [Exception [ , args [, traceback] ]
The type of exception in the statement is an exception (for example, the Nameerror) parameter is an exception parameter value. This parameter is optional and if not provided, the exception parameter is "None".
The last parameter is optional (rarely used in practice) and, if present, is the tracking exception object.
9. Procedures
An exception can be a string, a class, or an object. The Python kernel provides exceptions, most of which are instantiated classes, which are parameters of an instance of a class.
It is very simple to define an exception as follows:
1 #-*-coding:utf-8-*-2 #Defining Functions3 defMye (level):4 ifLevel < 1:5 RaiseException ("invalidlevel!", Level)6 #after the exception is triggered, the following code will no longer execute7 8 Try:9Mye (0.001)Ten except "invalidlevel!": One Print1 A Else: - Print2
10. Effects
11. Custom Exceptions
By creating a new Exception class, programs can name their own exceptions. Exceptions should be typical of inheriting from the exception class, either directly or indirectly.
The following is an example of a runtimeerror-related instance in which a class is created and the base class is RuntimeError, which is used to output more information when the exception is triggered.
In the TRY statement block, after the user-defined exception executes the EXCEPT block statement, the variable e is used to create an instance of the Networkerror class.
Exception handling in Python