Exception handling is often used in programming. The exception handling in Python is summarized as follows: it is similar to the usage in other languages, but it is different.
1. Exception handling:
Catch exceptions with try/except statements
The try/except block is used to detect errors in the try block, so that the except statement catches the exception and handles it.
grammar:
try: <statement> # code that may cause exceptions except <name>: <statement># catch exception named name
How the try statement works: When you start a try statement, Python marks it in the context of the current program, so that it will come back here when an exception occurs, the try clause is executed first, and what happens next depends on whether the exception occurred or not. :
1) 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. raises a new exception).
2) If an exception occurs in the statement after the try, but there is no matching except clause, the exception will be delivered to the upper try, or to the top level of the program (this will end the program and print the default error message) .
3) If no exception occurs when the try clause executes, python will execute the statement following the else statement (if there is an else), and then control flow through the entire try statement
The same processing statement can handle multiple exception types, just put them in a tuple
grammar:
try: <statement> except(Error1,Error2,Error3): <statement>
You can also create multiple processing statements for multiple exceptions
try: <statement> except(name1): <statement> except(name2): <statement> except(name3): <statement>
2. try—finally statement
Regardless of whether an exception occurs, the final statement will be executed, such as: closing the file, releasing the lock, and returning the database connection to the connection pool.
grammar:
try: <statement> finally: <statement># will always execute when exiting try
3.raise throws an exception:
grammar:
raise [Exception [, args [, traceback]]]
The statement where Exception is the type of exception (for example, NameError) parameter is an exception parameter value. This parameter is optional, if not provided, the exception's parameter is "None". The last parameter is optional (rarely used in practice) and, if present, is the trace exception object.
def buy_goods( count ): if count < 0: raise Exception("The purchase quantity cannot be less than 0!") # After the exception is triggered, the following code will not be executed again
4. User-defined exception:
By creating a new exception class, programs can name their own exceptions. Exceptions should typically inherit from the Exception class, either directly or indirectly.
class Networkerror(RuntimeError): def __init__(self, arg): self.args = arg
Create a class whose base class is RuntimeError
After defining the above class, you can trigger the exception
try: raise Networkerror("Bad hostname") except Networkerror,e: print e.args
The variable e is used to create an instance of the Networkerror class.