Abnormal parameters
An exception can bring parameter, the parameter information can be outputted as an abnormality.
You can capture parameters by abnormal except statement as follows:
An abnormal value of the variable in the received typically contains exception statement. You may receive one or more variable values in the form of tuples.
Tuple typically contains the error string, error number, position error.
Examples
The following is an example of a single exception:
The above program execution results are as follows:
Trigger abnormal
We can use your own trigger abnormal raise statement
raise syntax is as follows:
Exception statements type is abnormal (e.g., NameError) any one of parameters of the standard exception, args abnormal parameter is supplied own.
The last parameter is optional (rarely used in practice), if present, is tracking the exception object.
Examples
An exception may be a string, a class or object. Python exception of the kernel, mostly large http://www.xuanhe.net/ instance of the class, which is an example of a class parameter.
Very simple definition of an exception, as follows:
Note: To be able to catch the exception, "except" statement must be useful to throw the same exception class object or string.
For example, we capture more than an exception, "except" statement is as follows:
Examples
Execution code above, the output is:
User-defined exceptions
By creating a new exception class, the program can name their own exceptions. Typical exceptions should be inherited from the Exception class, either directly or indirectly.
The following are examples of related RuntimeError, it creates a class instance, for the base class RuntimeError, for outputting additional information when an exception is triggered.
In the try block, the user-defined exception statement is executed except block, the variable e is used to create a class instance Networkerror.
After you define the above classes, you can trigger the exception, as follows: