Current location - Quotes Website - Team slogan - Complete slogan set of exception handling statements
Complete slogan set of exception handling statements
What is abnormal?

1. Error

As far as software is concerned, errors are grammatical or logical. Errors are grammatical or logical.

Grammatical errors indicate that there is a structural error in the software, which cannot be explained by the interpreter and compiled by the compiler. These errors must be corrected before the program is executed.

When the syntax of the program is correct, the rest is logical error. Logical errors may be caused by incomplete or illegal input;

In other cases, the logic may not be generated, calculated, or the process required to output the result may not be performed. These errors are usually called domain errors and range errors, respectively.

When python detects an error, the python interpreter will point out that the current stream can no longer be executed. At this time, an exception occurred.

2. abnormal

The best description of an exception is that it is an action taken outside the normal control flow due to an error in the program.

This behavior is divided into two stages: first, the error that causes the exception, and then the detection (and possible measures) stage.

The first stage occurs after the occurrence of an abnormal situation (sometimes called an abnormal situation).

As long as an error is detected and an exception is realized, an exception will appear in the interpreter. Trigger can also be called trigger, throw or generate. Through it, the interpreter informs the current control flow that an error has occurred.

Python also allows programmers to throw exceptions themselves. Whether caused by a python interpreter or a programmer, an exception is a signal that something is wrong.

The current stream will be interrupted to handle this error and take corresponding measures. This is the second stage.

Exception handling occurs in the second stage. After an exception is thrown, many different operations can be called.

You can ignore the error (record the error without taking any measures, and terminate the program after taking remedial measures. ) or try to continue the program after alleviating the impact of the problem.

All these operations are extensions or branches of control. The key is that the programmer can instruct the program how to execute when an error occurs.

Python uses exception objects to represent exceptions. When an error is encountered, an exception is thrown.

If the abnormal object is not handled or caught, the program will terminate its execution through so-called backtracking.

exception handling

You can use the try/except statement to catch exceptions.

The try/except statement is used to detect errors in the try statement block, so that the except statement can capture abnormal information and handle it.

If you don't want to end your program when an exception occurs, just catch it in try.

Grammar:

The following is a simple syntax of try try ... Except ... Otherwise:

The working principle of Try is that when a try statement is started, python will mark it in the context of the current program, so that when an exception occurs, it can come back here. The try clause is executed first, and what happens next depends on whether an exception occurs during execution.

If an exception occurs while executing the statement after try, python will jump back to try and execute the first exception clause that matches the exception. After handling the exception, the control flow will traverse the entire try statement (unless a new exception is thrown when handling the exception).

If an exception occurs in the statement after try, but there is no matching except clause, the exception will be submitted to the top of the try or program above (this will end the program and print the default error message).

If no exception occurs during the execution of the try clause, python will execute the statement after the else statement (if there is an else statement), and then control the flow through the entire try statement.

Use except without any exception types.

You can use except without any exception type, as shown in the following example:

In this way, the try-except statement captures all the exceptions that occur. But this is not a good way, and we can't identify specific abnormal information through this program. Because it catches all exceptions.