8. Errors And Exceptions — Python 3.10.6 Documentation
Có thể bạn quan tâm
8.2. Exceptions¶
Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions and are not unconditionally fatal: you will soon learn how to handle them in Python programs. Most exceptions are not handled by programs, however, and result in error messages as shown here:
>>> 10 * (1/0) Traceback (most recent call last): File "<stdin>", line 1, in <module> 10 * (1/0) ~^~ ZeroDivisionError: division by zero >>> 4 + spam*3 Traceback (most recent call last): File "<stdin>", line 1, in <module> 4 + spam*3 ^^^^ NameError: name 'spam' is not defined >>> '2' + 2 Traceback (most recent call last): File "<stdin>", line 1, in <module> '2' + 2 ~~~~^~~ TypeError: can only concatenate str (not "int") to strThe last line of the error message indicates what happened. Exceptions come in different types, and the type is printed as part of the message: the types in the example are ZeroDivisionError, NameError and TypeError. The string printed as the exception type is the name of the built-in exception that occurred. This is true for all built-in exceptions, but need not be true for user-defined exceptions (although it is a useful convention). Standard exception names are built-in identifiers (not reserved keywords).
The rest of the line provides detail based on the type of exception and what caused it.
The preceding part of the error message shows the context where the exception occurred, in the form of a stack traceback. In general it contains a stack traceback listing source lines; however, it will not display lines read from standard input.
Built-in Exceptions lists the built-in exceptions and their meanings.
Từ khóa » Try Và Except Trong Python
-
[Tự Học Python] Try Và Except Trong Python »
-
Xử Lý Ngoại Lệ - Exception Handling Trong Python
-
Xử Lý Ngoại Lệ Trong Python - Học Lập Trình Python - Viettuts
-
Bài 17. Xử Lý Lỗi Trong Python (Try - Except)
-
Hướng Dẫn Xử Lý Ngoại Lệ Trong Python - Openplanning
-
Lỗi Và Xử Lý Ngoại Lệ Trong Python - Viblo
-
[PY101] 3.4 - Try Và Except Trong Python - YouTube
-
Xử Lý Lỗi Ngoại Lệ Trong Python (Try...Except) - Yêu Lập Trình
-
Xử Lý Ngoại Lệ (Try Except) Trong Python
-
Exception Trong Python
-
Exceptions Trong Python, Xử Lý Lỗi đơn Giản - Freetuts
-
Hướng Dẫn Xử Lý Exception Trong Python - NIIT - ICT Hà Nội
-
Xử Lý Ngoại Lệ (Exception Handling) Trong Python - TEK4
-
ĐỪNG NÊN LẠM DỤNG TRY EXCEPT TRONG PYTHON