Hướng dẫn what causes oserror in python? - điều gì gây ra lỗi oserror trong python?

Hướng dẫn what causes oserror in python? - điều gì gây ra lỗi oserror trong python?
Shubham Singh Kshatriya

Tổng quan

os.error trong Python là lớp lỗi cho tất cả các lỗi I/O và là bí danh của ngoại lệ Oserror. Tất cả các phương thức có trong mô -đun HĐH sẽ tăng ngoại lệ os.error khi đường dẫn tệp không thể truy cập hoặc không hợp lệ.

Thí dụ

Hãy xem một ví dụ về os.error trong đoạn mã bên dưới:

# import os module

import os

try:

# file path

filePath = 'demo.txt'

# using os.open() to open the file

fileDescriptor = os.open(filePath, os.O_RDWR)

# using os.close() to close the file descriptor

os.close(fileDescriptor)

except os.error:

print("Error in file:", filePath)

Một ví dụ về Os.Error trong Python

Giải trình

  • Dòng 2: Chúng tôi nhập mô -đun os.
  • Dòng 4: Chúng tôi sử dụng khối
    raise new_exc from original_exc
    
    0 để thực thi các phương thức HĐH.
  • Dòng 6: Chúng tôi khai báo một biến,
    raise new_exc from original_exc
    
    1, chứa đường dẫn tệp.
  • Dòng 9: Chúng tôi sử dụng phương thức
    raise new_exc from original_exc
    
    2 để mở tệp và tạo bộ mô tả tệp của nó.
  • Dòng 12: Chúng tôi sử dụng phương thức
    raise new_exc from original_exc
    
    3 để đóng bộ mô tả tệp.
  • Dòng 14: Chúng tôi sử dụng khối
    raise new_exc from original_exc
    
    4 để bắt bất kỳ ngoại lệ os.error nào.

Đầu ra

Khi chúng tôi cố gắng mở tệp

raise new_exc from original_exc
6 trong dòng 9, một ngoại lệ os.error sẽ được nêu ra. Ngoại lệ này sẽ bị bắt trong khối
raise new_exc from original_exc
4. Sau đó, chúng tôi sẽ in
raise new_exc from original_exc
9 vào bảng điều khiển.

THẺ LIÊN QUAN

Python

lỗi

ngoại lệ

cộng đồng

Người đóng góp

Shubham Singh Kshatriya

Trong Python, tất cả các trường hợp ngoại lệ phải là trường hợp của một lớp bắt nguồn từ

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
0. Trong một tuyên bố
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
1 với mệnh đề
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
2 đề cập đến một lớp cụ thể, điều khoản đó cũng xử lý bất kỳ lớp ngoại lệ nào có nguồn gốc từ lớp đó (nhưng không phải là các lớp ngoại lệ mà nó có nguồn gốc). Hai lớp ngoại lệ không liên quan thông qua phân lớp không bao giờ tương đương, ngay cả khi chúng có cùng tên.

Các ngoại lệ tích hợp được liệt kê dưới đây có thể được tạo bởi trình thông dịch hoặc các chức năng tích hợp. Ngoại trừ khi được đề cập, họ có một giá trị liên quan của người Viking cho biết nguyên nhân chi tiết của lỗi. Đây có thể là một chuỗi hoặc một phần của một số mục thông tin (ví dụ: mã lỗi và một chuỗi giải thích mã). Giá trị liên quan thường được truyền dưới dạng đối số cho hàm tạo lớp ngoại lệ.

Mã người dùng có thể tăng các ngoại lệ tích hợp. Điều này có thể được sử dụng để kiểm tra một trình xử lý ngoại lệ hoặc để báo cáo tình trạng lỗi, giống như tình huống mà người phiên dịch tăng ngoại lệ tương tự; Nhưng hãy cẩn thận rằng không có gì để ngăn mã người dùng gây ra lỗi không phù hợp.

Các lớp ngoại lệ tích hợp có thể được phân lớp để xác định các ngoại lệ mới; Các lập trình viên được khuyến khích lấy các ngoại lệ mới từ lớp

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
3 hoặc một trong các lớp con của nó, chứ không phải từ
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
0. Thông tin thêm về việc xác định các ngoại lệ có sẵn trong hướng dẫn Python theo các ngoại lệ do người dùng xác định.User-defined Exceptions.

Bối cảnh ngoại lệ

Khi nêu ra một ngoại lệ mới trong khi một ngoại lệ khác đã được xử lý, thuộc tính ngoại lệ mới ____ ____25 được tự động đặt thành ngoại lệ được xử lý. Một ngoại lệ có thể được xử lý khi mệnh đề

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
2 hoặc
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
7 hoặc câu lệnh
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
8, được sử dụng.

Bối cảnh ngoại lệ ngầm này có thể được bổ sung một nguyên nhân rõ ràng bằng cách sử dụng

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
9 với
>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
0:

raise new_exc from original_exc

Biểu thức sau

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
9 phải là một ngoại lệ hoặc
>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
2. Nó sẽ được đặt dưới dạng
>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
3 trên ngoại lệ được nâng lên. Cài đặt
>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
3 cũng ngầm định thuộc tính
>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
5 thành
>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
6, do đó sử dụng
>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
7 thay thế hiệu quả ngoại lệ cũ bằng cái mới cho mục đích hiển thị (ví dụ: chuyển đổi
>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
8 thành
>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
9), trong khi để lại ngoại lệ cũ trong ____

Mã hiển thị theo dõi mặc định hiển thị các trường hợp ngoại lệ được chuỗi này ngoài bản nhúng cho chính ngoại lệ. Một ngoại lệ bị xích rõ ràng trong

>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
3 luôn được hiển thị khi có mặt. Một ngoại lệ bị xích ngầm trong
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
5 chỉ được hiển thị nếu
>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
3 là
>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
2 và
>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
5 là sai.

Trong cả hai trường hợp, bản thân ngoại lệ luôn được hiển thị sau bất kỳ trường hợp ngoại lệ nào để dòng cuối cùng của Traceback luôn cho thấy ngoại lệ cuối cùng được nêu ra.

Kế thừa từ các trường hợp ngoại lệ tích hợp

Mã người dùng có thể tạo các lớp con kế thừa từ một loại ngoại lệ. Nó khuyến nghị chỉ phân lớp một loại ngoại lệ tại một thời điểm để tránh mọi xung đột có thể xảy ra giữa cách các cơ sở xử lý thuộc tính

class Errors(ExceptionGroup):
   def __new__(cls, errors, exit_code):
      self = super().__new__(Errors, f"exit code: {exit_code}", errors)
      self.exit_code = exit_code
      return self

   def derive(self, excs):
      return Errors(excs, self.exit_code)
6, cũng như do khả năng không tương thích của bố cục bộ nhớ.

Chi tiết triển khai CPYThon: Hầu hết các trường hợp ngoại lệ tích hợp được thực hiện trong C để biết hiệu quả, xem: Đối tượng/ngoại lệ.C. Một số có bố cục bộ nhớ tùy chỉnh khiến cho không thể tạo một lớp con kế thừa từ nhiều loại ngoại lệ. Bố cục bộ nhớ của một loại là một chi tiết triển khai và có thể thay đổi giữa các phiên bản Python, dẫn đến xung đột mới trong tương lai. Do đó, nó khuyến nghị để tránh phân lớp nhiều loại ngoại lệ hoàn toàn. Most built-in exceptions are implemented in C for efficiency, see: Objects/exceptions.c. Some have custom memory layouts which makes it impossible to create a subclass that inherits from multiple exception types. The memory layout of a type is an implementation detail and might change between Python versions, leading to new conflicts in the future. Therefore, it’s recommended to avoid subclassing multiple exception types altogether.

Các lớp cơ sở

Các trường hợp ngoại lệ sau đây được sử dụng chủ yếu làm các lớp cơ sở cho các ngoại lệ khác.

ExceptionBaseexception¶ BaseException

Lớp cơ sở cho tất cả các trường hợp ngoại lệ tích hợp. Nó không có nghĩa là được kế thừa trực tiếp bởi các lớp do người dùng xác định (đối với đó, sử dụng

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
3). Nếu
class Errors(ExceptionGroup):
   def __new__(cls, errors, exit_code):
      self = super().__new__(Errors, f"exit code: {exit_code}", errors)
      self.exit_code = exit_code
      return self

   def derive(self, excs):
      return Errors(excs, self.exit_code)
8 được gọi trên một thể hiện của lớp này, thì việc biểu diễn (các) đối số cho trường hợp được trả về hoặc chuỗi trống khi không có đối số.

args¶

Các đối số của các đối số được đưa ra cho hàm tạo ngoại lệ. Một số trường hợp ngoại lệ tích hợp (như

class Errors(ExceptionGroup):
   def __new__(cls, errors, exit_code):
      self = super().__new__(Errors, f"exit code: {exit_code}", errors)
      self.exit_code = exit_code
      return self

   def derive(self, excs):
      return Errors(excs, self.exit_code)
9) mong đợi một số lượng đối số nhất định và gán một ý nghĩa đặc biệt cho các yếu tố của tuple này, trong khi những người khác thường chỉ được gọi với một chuỗi đơn đưa ra thông báo lỗi.

with_traceback (tb) ¶(tb)

Phương thức này đặt TB làm traceback mới cho ngoại lệ và trả về đối tượng ngoại lệ. Nó được sử dụng phổ biến hơn trước khi các tính năng chuỗi ngoại lệ của PEP 3134 có sẵn. Ví dụ sau đây cho thấy cách chúng ta có thể chuyển đổi một thể hiện

BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning
0 thành một ví dụ là
BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning
1 trong khi bảo tồn dấu vết. Sau khi được nâng lên, khung hiện tại được đẩy lên dấu vết của
BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning
1, như đã xảy ra với dấu vết của
BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning
0 ban đầu, chúng tôi đã cho phép nó tuyên truyền đến người gọi.PEP 3134 became available. The following example shows how we can convert an instance of
BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning
0 into an instance of
BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning
1 while preserving the traceback. Once raised, the current frame is pushed onto the traceback of the
BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning
1, as would have happened to the traceback of the original
BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning
0 had we allowed it to propagate to the caller.

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)

add_note (lưu ý) ¶(note)

Thêm chuỗi

BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning
4 vào các ghi chú ngoại lệ xuất hiện trong dấu vết tiêu chuẩn sau chuỗi ngoại lệ. Một
BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning
5 được nâng lên nếu
BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning
4 không phải là một chuỗi.

Mới trong phiên bản 3.11.

__notes__¶

Một danh sách các ghi chú của ngoại lệ này, được thêm vào với

BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning
7. Thuộc tính này được tạo khi
BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning
7 được gọi.

Mới trong phiên bản 3.11.

__notes__¶ Exception

Một danh sách các ghi chú của ngoại lệ này, được thêm vào với

BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning
7. Thuộc tính này được tạo khi
BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning
7 được gọi.

ngoại lệ Exexception¶ ArithmeticError

Tất cả các trường hợp ngoại lệ được tích hợp, phi hệ thống đều có nguồn gốc từ lớp này. Tất cả các trường hợp ngoại lệ do người dùng xác định cũng nên được lấy từ lớp này.

ngoại lệ BufferError

Lớp cơ sở cho các trường hợp ngoại lệ tích hợp được nêu ra cho các lỗi số học khác nhau:

BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning
9, os.error0, os.error1.buffer related operation cannot be performed.

ExceptionBufferErorror¶ LookupError

Lớn lên khi một hoạt động liên quan đến bộ đệm không thể được thực hiện.

ngoại lệ

Lớp cơ sở cho các ngoại lệ được nêu ra khi một khóa hoặc chỉ mục được sử dụng trên ánh xạ hoặc trình tự không hợp lệ: os.error2,

>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
8. Điều này có thể được nâng lên trực tiếp bởi os.error4.

Ngoại lệ cụ thểAssertionError

Các trường hợp ngoại lệ sau đây là các trường hợp ngoại lệ thường được nêu ra.

EXCECTERSERERERROROR¶AttributeError

Lớn lên khi một tuyên bố os.error5 thất bại.Attribute references) or assignment fails. (When an object does not support attribute references or attribute assignments at all,

BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning
5 is raised.)

ngoại lệ

Được nâng lên khi một tham chiếu thuộc tính (xem tham chiếu thuộc tính) hoặc gán không thành công. (Khi một đối tượng hoàn toàn không hỗ trợ tham chiếu thuộc tính hoặc gán thuộc tính,

BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning
5 được nâng lên.)Added the os.error7 and os.error8 attributes.

Các thuộc tính os.error7 và os.error8 có thể được đặt bằng cách sử dụng các đối số chỉ từ khóa cho hàm tạo. Khi đặt, chúng đại diện cho tên của thuộc tính đã được cố gắng truy cập và đối tượng được truy cập cho thuộc tính nói, tương ứng.EOFError

Đã thay đổi trong phiên bản 3.10: Đã thêm các thuộc tính os.error7 và os.error8.

ngoại lệFloatingPointError

Lớn lên khi hàm os.error1 đạt được điều kiện cuối tập tin (EOF) mà không cần đọc bất kỳ dữ liệu nào. .

ngoại lệGeneratorExit

Hiện không được sử dụng.generator or coroutine is closed; see os.error4 and os.error5. It directly inherits from

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
0 instead of
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
3 since it is technically not an error.

ngoại lệgeneratorexit¶ImportError

Lớn lên khi một máy phát điện hoặc coroutine được đóng lại; Xem os.error4 và os.error5. Nó trực tiếp kế thừa từ

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
0 thay vì
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
3 vì về mặt kỹ thuật không phải là một lỗi.

ngoại lệ

Đã thay đổi trong phiên bản 3.3: Đã thêm các thuộc tính os.error7 và os.error1.Added the os.error7 and os.error1 attributes.

ngoại lệModuleNotFoundError

Một lớp con của os.error4 được nâng lên bởi os.error8 khi không thể định vị được một mô -đun. Nó cũng được nâng lên khi

>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
2 được tìm thấy trong os.error7.

Mới trong phiên bản 3.6.

ngoại lệ Indloadexerror¶ IndexError

Lớn lên khi một chỉ số trình tự nằm ngoài phạm vi. .

ngoại lệKeyError

Lớn lên khi không tìm thấy khóa ánh xạ (từ điển) trong tập hợp các khóa hiện có.

ngoại lệ KeyboardInterrupt

Lớn lên khi người dùng nhấn phím ngắt (thường điều khiển-C hoặc xóa). Trong quá trình thực hiện, kiểm tra ngắt được thực hiện thường xuyên. Ngoại lệ kế thừa từ

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
0 để không vô tình bị bắt bởi mã bắt
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
3 và do đó ngăn người phiên dịch thoát ra.

Ghi chú

Bắt os1 đòi hỏi phải xem xét đặc biệt. Bởi vì nó có thể được nâng lên tại các điểm không thể đoán trước, trong một số trường hợp, nó có thể rời khỏi chương trình đang chạy ở trạng thái không nhất quán. Nói chung, tốt nhất là cho phép os1 kết thúc chương trình càng nhanh càng tốt hoặc tránh nâng cao hoàn toàn. (Xem ghi chú về bộ xử lý tín hiệu và ngoại lệ.)Note on Signal Handlers and Exceptions.)

ngoại lệMemoryError

Lớn lên khi một hoạt động hết bộ nhớ nhưng tình hình vẫn có thể được giải cứu (bằng cách xóa một số đối tượng). Giá trị liên quan là một chuỗi cho biết loại hoạt động (nội bộ) nào hết bộ nhớ. Lưu ý rằng vì kiến ​​trúc quản lý bộ nhớ cơ bản (hàm CTHER ____ ____93), trình thông dịch có thể không phải lúc nào cũng có thể phục hồi hoàn toàn khỏi tình huống này; Tuy nhiên, nó đặt ra một ngoại lệ để có thể in một dấu vết ngăn xếp, trong trường hợp một chương trình chạy là nguyên nhân.

ngoại lệNameError

Lớn lên khi không tìm thấy tên địa phương hoặc toàn cầu. Điều này chỉ áp dụng cho các tên không đủ tiêu chuẩn. Giá trị liên quan là một thông báo lỗi bao gồm tên không thể tìm thấy.

Thuộc tính os.error7 có thể được đặt bằng đối số chỉ từ khóa cho hàm tạo. Khi đặt nó biểu thị tên của biến đã được cố gắng truy cập.

Đã thay đổi trong phiên bản 3.10: Đã thêm thuộc tính os.error7.Added the os.error7 attribute.

ngoại lệNotImplementedError

Ngoại lệ này có nguồn gốc từ os6. Trong các lớp cơ sở do người dùng xác định, các phương thức trừu tượng sẽ nâng cao ngoại lệ này khi chúng yêu cầu các lớp dẫn xuất để ghi đè phương thức hoặc trong khi lớp đang được phát triển để chỉ ra rằng việc triển khai thực sự vẫn cần được thêm vào.

Ghi chú

Bắt os1 đòi hỏi phải xem xét đặc biệt. Bởi vì nó có thể được nâng lên tại các điểm không thể đoán trước, trong một số trường hợp, nó có thể rời khỏi chương trình đang chạy ở trạng thái không nhất quán. Nói chung, tốt nhất là cho phép os1 kết thúc chương trình càng nhanh càng tốt hoặc tránh nâng cao hoàn toàn. (Xem ghi chú về bộ xử lý tín hiệu và ngoại lệ.)

Ghi chú

Bắt os1 đòi hỏi phải xem xét đặc biệt. Bởi vì nó có thể được nâng lên tại các điểm không thể đoán trước, trong một số trường hợp, nó có thể rời khỏi chương trình đang chạy ở trạng thái không nhất quán. Nói chung, tốt nhất là cho phép os1 kết thúc chương trình càng nhanh càng tốt hoặc tránh nâng cao hoàn toàn. (Xem ghi chú về bộ xử lý tín hiệu và ngoại lệ.)

ngoại lệOSError([arg])exceptionOSError(errno, strerror[, filename[, winerror[, filename2]]])

Lớn lên khi một hoạt động hết bộ nhớ nhưng tình hình vẫn có thể được giải cứu (bằng cách xóa một số đối tượng). Giá trị liên quan là một chuỗi cho biết loại hoạt động (nội bộ) nào hết bộ nhớ. Lưu ý rằng vì kiến ​​trúc quản lý bộ nhớ cơ bản (hàm CTHER ____ ____93), trình thông dịch có thể không phải lúc nào cũng có thể phục hồi hoàn toàn khỏi tình huống này; Tuy nhiên, nó đặt ra một ngoại lệ để có thể in một dấu vết ngăn xếp, trong trường hợp một chương trình chạy là nguyên nhân.

ngoại lệ

Lớn lên khi không tìm thấy tên địa phương hoặc toàn cầu. Điều này chỉ áp dụng cho các tên không đủ tiêu chuẩn. Giá trị liên quan là một thông báo lỗi bao gồm tên không thể tìm thấy.

Thuộc tính os.error7 có thể được đặt bằng đối số chỉ từ khóa cho hàm tạo. Khi đặt nó biểu thị tên của biến đã được cố gắng truy cập.

Đã thay đổi trong phiên bản 3.10: Đã thêm thuộc tính os.error7.

ngoại lệ

Ngoại lệ này có nguồn gốc từ os6. Trong các lớp cơ sở do người dùng xác định, các phương thức trừu tượng sẽ nâng cao ngoại lệ này khi chúng yêu cầu các lớp dẫn xuất để ghi đè phương thức hoặc trong khi lớp đang được phát triển để chỉ ra rằng việc triển khai thực sự vẫn cần được thêm vào.

Không nên sử dụng để chỉ ra rằng một toán tử hoặc phương thức hoàn toàn không được hỗ trợ - trong trường hợp đó, để lại toán tử / phương thức không xác định hoặc, nếu một lớp con, đặt nó thành

>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
2.

os8 và os9 không thể hoán đổi cho nhau, mặc dù chúng có tên và mục đích tương tự. Xem os9 để biết chi tiết về thời điểm sử dụng.

ExceptionOserror ([arg]) Exceptionserror (errno, strerror [, filename [, nhà tù [, filename2]]]]))

Ngoại lệ này được nêu ra khi một chức năng hệ thống trả về một lỗi liên quan đến hệ thống, bao gồm các lỗi I/O như tệp không tìm thấy tệp hoặc đĩa đơn đầy đủ (không phải cho các loại đối số bất hợp pháp hoặc các lỗi ngẫu nhiên khác).filename2

Hình thức thứ hai của hàm tạo đặt các thuộc tính tương ứng, được mô tả dưới đây. Các thuộc tính mặc định là

>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
2 nếu không được chỉ định. Đối với khả năng tương thích ngược, nếu ba đối số được thông qua, thuộc tính
class Errors(ExceptionGroup):
   def __new__(cls, errors, exit_code):
      self = super().__new__(Errors, f"exit code: {exit_code}", errors)
      self.exit_code = exit_code
      return self

   def derive(self, excs):
      return Errors(excs, self.exit_code)
6 chỉ chứa 2-Tuple của hai đối số cấu trúc đầu tiên.

Đã thay đổi trong phiên bản 3.4: Thuộc tính

raise new_exc from original_exc
14 hiện là tên tệp gốc được truyền cho hàm, thay vì tên được mã hóa thành hoặc được giải mã từ bộ mã hóa hệ thống tệp và trình xử lý lỗi. Ngoài ra, đối số và thuộc tính của hàm tạo FileName2 đã được thêm vào.The
raise new_exc from original_exc
14 attribute is now the original file name passed to the function, instead of the name encoded to or decoded from the filesystem encoding and error handler. Also, the filename2 constructor argument and attribute was added.

ngoại lệ OverflowError

Lớn lên khi kết quả của một hoạt động số học là quá lớn để được thể hiện. Điều này không thể xảy ra đối với các số nguyên (thay vì tăng

raise new_exc from original_exc
18 hơn là từ bỏ). Tuy nhiên, vì lý do lịch sử, Overflowerror đôi khi được nuôi dưỡng cho các số nguyên nằm ngoài phạm vi yêu cầu. Do thiếu tiêu chuẩn hóa xử lý ngoại lệ điểm nổi trong C, hầu hết các hoạt động điểm nổi đều không được kiểm tra.

ngoại lệ rrecursionerror¶RecursionError

Ngoại lệ này có nguồn gốc từ os6. Nó được nâng lên khi thông dịch phát hiện rằng độ sâu đệ quy tối đa (xem

raise new_exc from original_exc
20) bị vượt quá.

Mới trong phiên bản 3.5: Trước đây, một os6 đã được nâng lên.Previously, a plain os6 was raised.

ExcepcesReferenceErrorror¶ReferenceError

Ngoại lệ này được nâng lên khi một proxy tham chiếu yếu, được tạo bởi hàm

raise new_exc from original_exc
22, được sử dụng để truy cập một thuộc tính của người giới thiệu sau khi nó được thu thập rác. Để biết thêm thông tin về các tài liệu tham khảo yếu, hãy xem mô -đun
raise new_exc from original_exc
23.

ngoại lệRuntimeError

Lớn lên khi phát hiện lỗi mà không rơi vào bất kỳ loại nào khác. Giá trị liên quan là một chuỗi chỉ ra những gì chính xác đã sai.

Ngoại lệ StopIteration

Được nâng lên bởi chức năng tích hợp

raise new_exc from original_exc
24 và phương thức
raise new_exc from original_exc
25 của trình lặp lại để báo hiệu rằng không có các mục nào được tạo ra bởi iterator.iterator's
raise new_exc from original_exc
25 method to signal that there are no further items produced by the iterator.

Đối tượng ngoại lệ có một thuộc tính duy nhất

raise new_exc from original_exc
26, được đưa ra như một đối số khi xây dựng ngoại lệ và mặc định là
>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
2.

Khi một hàm tạo hoặc hàm coroutine trở lại, một thể hiện

raise new_exc from original_exc
28 mới được nâng lên và giá trị được trả về bởi hàm được sử dụng làm tham số
raise new_exc from original_exc
26 cho hàm tạo của ngoại lệ.generator or coroutine function returns, a new
raise new_exc from original_exc
28 instance is raised, and the value returned by the function is used as the
raise new_exc from original_exc
26 parameter to the constructor of the exception.

Nếu mã máy phát trực tiếp hoặc gián tiếp tăng

raise new_exc from original_exc
28, nó sẽ được chuyển đổi thành os6 (giữ lại
raise new_exc from original_exc
28 là nguyên nhân ngoại lệ mới).

Đã thay đổi trong phiên bản 3.3: Đã thêm thuộc tính

raise new_exc from original_exc
26 và khả năng cho các hàm của máy phát sử dụng để trả về một giá trị.Added
raise new_exc from original_exc
26 attribute and the ability for generator functions to use it to return a value.

Đã thay đổi trong phiên bản 3.5: Đã giới thiệu chuyển đổi RunTimeError thông qua

raise new_exc from original_exc
34, xem PEP 479.Introduced the RuntimeError transformation via
raise new_exc from original_exc
34, see PEP 479.

Đã thay đổi trong phiên bản 3.7: Bật PEP 479 cho tất cả mã theo mặc định: Lỗi

raise new_exc from original_exc
28 được nêu trong trình tạo được chuyển thành os6.Enable PEP 479 for all code by default: a
raise new_exc from original_exc
28 error raised in a generator is transformed into a os6.

ngoại lệstopasynciteration¶StopAsyncIteration

Phải được nâng lên bằng phương pháp

raise new_exc from original_exc
37 của một đối tượng lặp không đồng bộ để dừng lặp lại.asynchronous iterator object to stop the iteration.

Mới trong phiên bản 3.5.

ngoại lệSyntaxError(message, details)

Lớn lên khi trình phân tích cú pháp gặp lỗi cú pháp. Điều này có thể xảy ra trong một câu lệnh os.error8, trong một cuộc gọi đến các chức năng tích hợp

raise new_exc from original_exc
39,
raise new_exc from original_exc
40 hoặc
raise new_exc from original_exc
41 hoặc khi đọc tập lệnh ban đầu hoặc đầu vào tiêu chuẩn (cũng tương tác).

class Errors(ExceptionGroup):
   def __new__(cls, errors, exit_code):
      self = super().__new__(Errors, f"exit code: {exit_code}", errors)
      self.exit_code = exit_code
      return self

   def derive(self, excs):
      return Errors(excs, self.exit_code)
8 của trường hợp ngoại lệ chỉ trả về thông báo lỗi. Chi tiết là một tuple mà các thành viên cũng có sẵn dưới dạng các thuộc tính riêng biệt.

Tên tệp

Tên của tệp Lỗi cú pháp xảy ra trong.

vải mỏng

Số dòng nào trong tệp xảy ra lỗi. Đây là 1 chỉ số: dòng đầu tiên trong tệp có

raise new_exc from original_exc
43 là 1.

bù lại¶

Cột trong dòng xảy ra lỗi. Đây là 1 chỉ số: ký tự đầu tiên trong dòng có

raise new_exc from original_exc
44 là 1.

chữ¶

Văn bản mã nguồn liên quan đến lỗi.

end_lineno¶

Số dòng nào trong tệp Lỗi xảy ra kết thúc. Đây là 1 chỉ số: dòng đầu tiên trong tệp có

raise new_exc from original_exc
43 là 1.

end_offset¶

Cột trong dòng cuối nơi xảy ra lỗi kết thúc. Đây là 1 chỉ số: ký tự đầu tiên trong dòng có

raise new_exc from original_exc
44 là 1.

Đối với các lỗi trong các trường F-String, thông báo được tiền tố bởi chuỗi F F: và các độ lệch được đặt trong một văn bản được xây dựng từ biểu thức thay thế. Ví dụ: biên dịch F F hèBad {A B} Trường kết quả trong thuộc tính Args này: (‘F-String: Hồi, (‘, 1, 2, ‘

ngoại lệ vô cùng IndentationError

Lớp cơ sở cho các lỗi cú pháp liên quan đến thụt không chính xác. Đây là một lớp con của

raise new_exc from original_exc
47.

ngoại lệ TabError

Lớn lên khi thụt lề chứa việc sử dụng các tab và không gian không nhất quán. Đây là một lớp con của

raise new_exc from original_exc
48.

ExceptionSystemErrorror¶ SystemError

Lớn lên khi thông dịch viên tìm thấy một lỗi nội bộ, nhưng tình huống không quá nghiêm trọng để khiến nó từ bỏ tất cả hy vọng. Giá trị liên quan là một chuỗi chỉ ra những gì đã sai (theo thuật ngữ cấp thấp).

Bạn nên báo cáo điều này cho tác giả hoặc người bảo trì thông dịch viên Python của bạn. Hãy chắc chắn báo cáo phiên bản của trình thông dịch Python (

raise new_exc from original_exc
49; nó cũng được in khi bắt đầu phiên Python tương tác), thông báo lỗi chính xác (giá trị liên quan ngoại lệ) và nếu có thể là nguồn của chương trình kích hoạt lỗi.

ngoại lệ hệ thốngSystemExit

Ngoại lệ này được nâng lên bởi hàm

raise new_exc from original_exc
50. Nó kế thừa từ
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
0 thay vì
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
3 để nó không vô tình bị bắt bởi mã bắt
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
3. Điều này cho phép ngoại lệ để tuyên truyền đúng và khiến thông dịch viên thoát ra. Khi nó không được xử lý, thông dịch viên Python thoát ra; Không có dấu vết ngăn xếp được in. Trình xây dựng chấp nhận cùng một đối số tùy chọn được chuyển đến
raise new_exc from original_exc
50. Nếu giá trị là một số nguyên, nó chỉ định trạng thái thoát hệ thống (được chuyển cho hàm CTHER
raise new_exc from original_exc
55); Nếu đó là
>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
2, trạng thái thoát bằng không; Nếu nó có một loại khác (chẳng hạn như một chuỗi), giá trị của đối tượng được in và trạng thái thoát là một.

Một cuộc gọi đến

raise new_exc from original_exc
50 được dịch thành một ngoại lệ để các trình xử lý dọn dẹp (các điều khoản ____27 của các câu lệnh
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
1) có thể được thực thi và để một trình gỡ lỗi có thể thực thi tập lệnh mà không có nguy cơ mất kiểm soát. Hàm
raise new_exc from original_exc
60 có thể được sử dụng nếu nó hoàn toàn cần thiết để thoát ngay lập tức (ví dụ: trong quy trình con sau cuộc gọi đến
raise new_exc from original_exc
61).

mã số¶

Trạng thái thoát hoặc thông báo lỗi được truyền cho hàm tạo. (Mặc định là

>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
2.)

ngoại lệ TypeError

Lớn lên khi một hoạt động hoặc chức năng được áp dụng cho một đối tượng thuộc loại không phù hợp. Giá trị liên quan là một chuỗi cung cấp chi tiết về loại không khớp.

Ngoại lệ này có thể được tăng lên bởi mã người dùng để chỉ ra rằng một hoạt động đã cố gắng trên một đối tượng không được hỗ trợ và không có nghĩa là. Nếu một đối tượng có nghĩa là hỗ trợ một hoạt động nhất định nhưng chưa cung cấp triển khai, os8 là ngoại lệ thích hợp để nâng cao.

Truyền các đối số của loại sai (ví dụ: vượt qua

raise new_exc from original_exc
64 khi dự kiến ​​
raise new_exc from original_exc
65) sẽ dẫn đến
BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning
5, nhưng chuyển các đối số có giá trị sai (ví dụ: một số bên ngoài ranh giới dự kiến) sẽ dẫn đến
raise new_exc from original_exc
67.

ngoại lệUnboundLocalError

Lớn lên khi một tham chiếu được thực hiện cho một biến cục bộ trong một hàm hoặc phương thức, nhưng không có giá trị nào bị ràng buộc với biến đó. Đây là một lớp con của

raise new_exc from original_exc
68.

ngoại lệUnicodeError

Lớn lên khi xảy ra lỗi mã hóa hoặc giải mã liên quan đến Unicode. Nó là một lớp con của

raise new_exc from original_exc
67.

raise new_exc from original_exc
70 có các thuộc tính mô tả lỗi mã hóa hoặc giải mã. Ví dụ,
raise new_exc from original_exc
71 cung cấp đầu vào không hợp lệ cụ thể mà codec không thành công.

mã hóa Or

Tên của mã hóa đã nêu ra lỗi.

lý do¶

Một chuỗi mô tả lỗi codec cụ thể.

sự vật¶

Đối tượng mà codec đang cố gắng mã hóa hoặc giải mã.

bắt đầu¶

Chỉ số đầu tiên của dữ liệu không hợp lệ trong

raise new_exc from original_exc
72.

chấm dứt¶

Chỉ mục sau dữ liệu không hợp lệ cuối cùng trong

raise new_exc from original_exc
72.

ngoại lệ UnicodeEncodeError

Lớn lên khi xảy ra lỗi liên quan đến Unicode trong quá trình mã hóa. Nó là một lớp con của

raise new_exc from original_exc
70.

ngoại lệ UnicodeDecodeError

Lớn lên khi xảy ra lỗi liên quan đến Unicode trong quá trình giải mã. Nó là một lớp con của

raise new_exc from original_exc
70.

ngoại lệ UnicodeTranslateError

Lớn lên khi xảy ra lỗi liên quan đến Unicode trong quá trình dịch. Nó là một lớp con của

raise new_exc from original_exc
70.

ngoại lệ giá trị ValueError

Lớn lên khi một hoạt động hoặc chức năng nhận được một đối số có loại đúng nhưng giá trị không phù hợp và tình huống không được mô tả bởi một ngoại lệ chính xác hơn như os.error2.

ngoại lệZeroDivisionError

Nêu ra khi đối số thứ hai của một phân chia hoặc hoạt động modulo bằng không. Giá trị liên quan là một chuỗi chỉ ra loại toán hạng và hoạt động.

Các ngoại lệ sau đây được giữ để tương thích với các phiên bản trước; Bắt đầu từ Python 3.3, chúng là bí danh của

class Errors(ExceptionGroup):
   def __new__(cls, errors, exit_code):
      self = super().__new__(Errors, f"exit code: {exit_code}", errors)
      self.exit_code = exit_code
      return self

   def derive(self, excs):
      return Errors(excs, self.exit_code)
9.

ExceptionEnv môi trườngEnvironmentErrorexceptionIOErrorexceptionWindowsError

Chỉ có sẵn trên Windows.

Ngoại lệ hệ điều hành

Các ngoại lệ sau đây là các lớp con của

class Errors(ExceptionGroup):
   def __new__(cls, errors, exit_code):
      self = super().__new__(Errors, f"exit code: {exit_code}", errors)
      self.exit_code = exit_code
      return self

   def derive(self, excs):
      return Errors(excs, self.exit_code)
9, chúng được nâng lên tùy thuộc vào mã lỗi hệ thống.

ngoại lệ BlockingIOError

Lớn lên khi một thao tác sẽ chặn trên một đối tượng (ví dụ: ổ cắm) được đặt cho hoạt động không chặn. Tương ứng với

raise new_exc from original_exc
04
raise new_exc from original_exc
81,
raise new_exc from original_exc
82,
raise new_exc from original_exc
83 và
raise new_exc from original_exc
84.

Ngoài các

class Errors(ExceptionGroup):
   def __new__(cls, errors, exit_code):
      self = super().__new__(Errors, f"exit code: {exit_code}", errors)
      self.exit_code = exit_code
      return self

   def derive(self, excs):
      return Errors(excs, self.exit_code)
9,
raise new_exc from original_exc
86 có thể có thêm một thuộc tính:

ký tự_written¶

Một số nguyên chứa số lượng ký tự được ghi vào luồng trước khi nó chặn. Thuộc tính này có sẵn khi sử dụng các lớp I/O được đệm từ mô -đun

raise new_exc from original_exc
87.

ngoại lệ ChildProcessError

Lớn lên khi một hoạt động trên một quá trình trẻ em không thành công. Tương ứng với

raise new_exc from original_exc
04
raise new_exc from original_exc
89.

ngoại lệConnectionErrorror¶ ConnectionError

Một lớp cơ sở cho các vấn đề liên quan đến kết nối.

Các lớp con là

raise new_exc from original_exc
90,
raise new_exc from original_exc
91,
raise new_exc from original_exc
92 và
raise new_exc from original_exc
93.

ngoại lệBrokenPipeError

Một lớp con của

raise new_exc from original_exc
94, được nâng lên khi cố gắng viết trên một đường ống trong khi đầu kia đã bị đóng, hoặc cố gắng viết trên một ổ cắm đã bị tắt để viết. Tương ứng với
raise new_exc from original_exc
04
raise new_exc from original_exc
96 và
raise new_exc from original_exc
97.

ngoại lệConnectionAbortedError¶ ConnectionAbortedError

Một lớp con của

raise new_exc from original_exc
94, được nêu ra khi một nỗ lực kết nối bị hủy bỏ bởi người ngang hàng. Tương ứng với
raise new_exc from original_exc
04
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
00.

ngoại lệConnectionRefusSeRrorror¶ConnectionRefusedError

Một lớp con của

raise new_exc from original_exc
94, được nêu ra khi một nỗ lực kết nối bị từ chối bởi người ngang hàng. Tương ứng với
raise new_exc from original_exc
04
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
03.

ngoại lệConnectionReseterror¶ConnectionResetError

Một lớp con của

raise new_exc from original_exc
94, được nâng lên khi kết nối được đặt lại bởi người ngang hàng. Tương ứng với
raise new_exc from original_exc
04
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
06.

ngoại lệFileeSerRorror¶FileExistsError

Lớn lên khi cố gắng tạo một tệp hoặc thư mục đã tồn tại. Tương ứng với

raise new_exc from original_exc
04
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
08.

Ngoại lệFilenotFounderrorror¶FileNotFoundError

Lớn lên khi một tệp hoặc thư mục được yêu cầu nhưng không tồn tại. Tương ứng với

raise new_exc from original_exc
04
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
10.

Ngoại lệ ContressedErrorror¶InterruptedError

Lớn lên khi một cuộc gọi hệ thống bị gián đoạn bởi tín hiệu đến. Tương ứng với

raise new_exc from original_exc
04
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
12.

Đã thay đổi trong phiên bản 3.5: Python hiện gọi lại các cuộc gọi hệ thống khi một hệ thống bị gián đoạn bởi tín hiệu, ngoại trừ nếu trình xử lý tín hiệu tăng ngoại lệ (xem PEP 475 cho lý do), thay vì tăng

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
13.Python now retries system calls when a syscall is interrupted by a signal, except if the signal handler raises an exception (see PEP 475 for the rationale), instead of raising
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
13.

ngoại lệ IsADirectoryError

Lớn lên khi một hoạt động tệp (chẳng hạn như

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
14) được yêu cầu trên một thư mục. Tương ứng với
raise new_exc from original_exc
04
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
16.

ngoại lệNotADirectoryError

Lớn lên khi một hoạt động thư mục (chẳng hạn như

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
17) được yêu cầu trên một cái gì đó không phải là một thư mục. Trên hầu hết các nền tảng POSIX, nó cũng có thể được nêu ra nếu một hoạt động cố gắng mở hoặc đi qua một tệp không trực tiếp như thể nó là một thư mục. Tương ứng với
raise new_exc from original_exc
04
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
19.

ExcencermissionErrorror¶PermissionError

Lớn lên khi cố gắng chạy một hoạt động mà không có quyền truy cập đầy đủ - ví dụ như các quyền hệ thống tập tin. Tương ứng với

raise new_exc from original_exc
04
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
21,
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
22 và
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
23.

ngoại lệProcessLookupError

Lớn lên khi một quá trình nhất định không tồn tại. Tương ứng với

raise new_exc from original_exc
04
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
25.

ngoại lệTimeouterror¶TimeoutError

Lớn lên khi một hàm hệ thống được định thời ở cấp hệ thống. Tương ứng với

raise new_exc from original_exc
04
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
27.

Mới trong phiên bản 3.3: Tất cả các lớp con

class Errors(ExceptionGroup):
   def __new__(cls, errors, exit_code):
      self = super().__new__(Errors, f"exit code: {exit_code}", errors)
      self.exit_code = exit_code
      return self

   def derive(self, excs):
      return Errors(excs, self.exit_code)
9 trên đã được thêm vào.All the above
class Errors(ExceptionGroup):
   def __new__(cls, errors, exit_code):
      self = super().__new__(Errors, f"exit code: {exit_code}", errors)
      self.exit_code = exit_code
      return self

   def derive(self, excs):
      return Errors(excs, self.exit_code)
9 subclasses were added.

Xem thêm

PEP 3151 - Làm lại hệ thống phân cấp ngoại lệ OS và IO - Reworking the OS and IO exception hierarchy

Cảnh báo Jo

Các trường hợp ngoại lệ sau đây được sử dụng làm loại cảnh báo; Xem tài liệu danh mục cảnh báo để biết thêm chi tiết.Warning Categories documentation for more details.

ngoại lệWarning

Lớp cơ sở cho các loại cảnh báo.

ExceptionUserWarning¶UserWarning

Lớp cơ sở cho các cảnh báo được tạo bởi mã người dùng.

ExceptionDepRecationWarning¶DeprecationWarning

Lớp cơ sở cho các cảnh báo về các tính năng không dùng nữa khi những cảnh báo đó được dành cho các nhà phát triển Python khác.

Bị bỏ qua bởi các bộ lọc cảnh báo mặc định, ngoại trừ trong mô -đun ____2229 (PEP 565). Cho phép chế độ phát triển Python cho thấy cảnh báo này.PEP 565). Enabling the Python Development Mode shows this warning.

Chính sách khấu hao được mô tả trong PEP 387.PEP 387.

ExceptionpendsDeprecationWarning¶PendingDeprecationWarning

Lớp cơ sở cho các cảnh báo về các tính năng lỗi thời và dự kiến ​​sẽ không được phản đối trong tương lai, nhưng hiện tại không bị phản đối.

Lớp này hiếm khi được sử dụng vì phát ra cảnh báo về việc phản đối sắp tới có thể là không bình thường và

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
30 được ưa thích cho việc khấu hao đã hoạt động.

Bị bỏ qua bởi các bộ lọc cảnh báo mặc định. Cho phép chế độ phát triển Python cho thấy cảnh báo này.Python Development Mode shows this warning.

Chính sách khấu hao được mô tả trong PEP 387.PEP 387.

ExceptionpendsDeprecationWarning¶ SyntaxWarning

Lớp cơ sở cho các cảnh báo về các tính năng lỗi thời và dự kiến ​​sẽ không được phản đối trong tương lai, nhưng hiện tại không bị phản đối.

Lớp này hiếm khi được sử dụng vì phát ra cảnh báo về việc phản đối sắp tới có thể là không bình thường và
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
30 được ưa thích cho việc khấu hao đã hoạt động.
RuntimeWarning

Bị bỏ qua bởi các bộ lọc cảnh báo mặc định. Cho phép chế độ phát triển Python cho thấy cảnh báo này.

ngoại lệFutureWarning

Lớp cơ sở cho cảnh báo về cú pháp đáng ngờ.

ngoại lệImportWarning

Lớp cơ sở cho các cảnh báo về hành vi thời gian chạy đáng ngờ.

Bị bỏ qua bởi các bộ lọc cảnh báo mặc định. Cho phép chế độ phát triển Python cho thấy cảnh báo này.Python Development Mode shows this warning.

ngoại lệUnicodeWarning

Lớp cơ sở cho cảnh báo về cú pháp đáng ngờ.

ngoại lệ EncodingWarning

Lớp cơ sở cho các cảnh báo về hành vi thời gian chạy đáng ngờ.

Ngoại lệOpt-in EncodingWarning for details.

Lớp cơ sở cho các cảnh báo về các tính năng không dùng nữa khi những cảnh báo đó được dành cho người dùng cuối của các ứng dụng được viết bằng Python.

ngoại lệBytesWarning

Lớp cơ sở cho các cảnh báo về các lỗi có thể xảy ra trong nhập khẩu mô -đun.

ngoại lệResourceWarning

Lớp cơ sở cho các cảnh báo liên quan đến Unicode.

Bị bỏ qua bởi các bộ lọc cảnh báo mặc định. Cho phép chế độ phát triển Python cho thấy cảnh báo này.Python Development Mode shows this warning.

ngoại lệ

Lớp cơ sở cho cảnh báo về cú pháp đáng ngờ.

ngoại lệ

Lớp cơ sở cho các cảnh báo về hành vi thời gian chạy đáng ngờ. ExceptionGroup(msg, excs)exceptionBaseExceptionGroup(msg, excs)

Cả hai loại ngoại lệ này bao gồm các ngoại lệ trong chuỗi

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
35. Tham số
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
36 phải là một chuỗi. Sự khác biệt giữa hai lớp là
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
37 mở rộng
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
0 và nó có thể bao gồm bất kỳ ngoại lệ nào, trong khi
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
39 mở rộng
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
3 và nó chỉ có thể bao bọc các lớp con của
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
3. Thiết kế này là để
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
42 bắt được
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
39 nhưng không phải
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
37.

Trình xây dựng

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
37 trả về
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
39 chứ không phải là
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
37 nếu tất cả các trường hợp ngoại lệ có chứa là các trường hợp
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
3, do đó nó có thể được sử dụng để làm cho lựa chọn tự động. Mặt khác, hàm tạo ____239 sẽ tăng
BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning
5 nếu bất kỳ ngoại lệ nào không phải là một lớp con
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
3.

thông điệp¶

Đối số

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
36 cho hàm tạo. Đây là một thuộc tính chỉ đọc.

ngoại lệ ha

Một bộ ngoại lệ trong chuỗi

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
35 được cung cấp cho hàm tạo. Đây là một thuộc tính chỉ đọc.

Nhóm phụ (điều kiện) ¶(condition)

Trả về một nhóm ngoại lệ chỉ chứa các ngoại lệ từ nhóm hiện tại phù hợp với điều kiện hoặc

>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
2 nếu kết quả trống.

Điều kiện có thể là một hàm chấp nhận một ngoại lệ và trả về đúng cho những người nên nằm trong nhóm con hoặc nó có thể là một loại ngoại lệ hoặc một loại ngoại lệ, được sử dụng để kiểm tra một trận đấu bằng cách sử dụng cùng một kiểm tra mà được sử dụng trong một mệnh đề

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
2.

Cấu trúc làm tổ của ngoại lệ hiện tại được bảo tồn trong kết quả, cũng như các giá trị của các trường

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
56,
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
57,
>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
3,
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
5 và
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
60. Các nhóm lồng rỗng được bỏ qua từ kết quả.

Điều kiện được kiểm tra tất cả các trường hợp ngoại lệ trong nhóm ngoại lệ lồng nhau, bao gồm các nhóm cấp cao nhất và bất kỳ nhóm ngoại lệ nào. Nếu điều kiện đúng với một nhóm ngoại lệ như vậy, nó được bao gồm trong kết quả đầy đủ.

Chia (điều kiện) ¶(condition)

Giống như

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
61, nhưng trả về cặp
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
62 trong đó
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
63 là
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
64 và
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
65 là phần không phù hợp còn lại.

xuất phát (excs) ¶(excs)

Trả về một nhóm ngoại lệ với cùng

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
56,
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
57,
>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))
3,
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
5 và
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
60 nhưng bao gồm các ngoại lệ trong
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
35.

Phương pháp này được sử dụng bởi

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
61 và
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
73. Một lớp con cần phải ghi đè nó để tạo các phiên bản trả về
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
61 và
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
73 của lớp con thay vì
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
39.

>>> class MyGroup(ExceptionGroup):
...     def derive(self, exc):
...         return MyGroup(self.message, exc)
...
>>> MyGroup("eg", [ValueError(1), TypeError(2)]).split(TypeError)
(MyGroup('eg', [TypeError(2)]), MyGroup('eg', [ValueError(1)]))

Lưu ý rằng

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
37 định nghĩa
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
78, do đó các lớp con cần một chữ ký cấu trúc khác cần phải ghi đè lên đó thay vì
try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)
79. Ví dụ: sau đây xác định một lớp con nhóm ngoại lệ chấp nhận EXIT_CODE và xây dựng thông báo nhóm từ nó.

class Errors(ExceptionGroup):
   def __new__(cls, errors, exit_code):
      self = super().__new__(Errors, f"exit code: {exit_code}", errors)
      self.exit_code = exit_code
      return self

   def derive(self, excs):
      return Errors(excs, self.exit_code)

Mới trong phiên bản 3.11.

Phân cấp ngoại lệ

Phân cấp lớp cho các trường hợp ngoại lệ tích hợp là:

BaseException
 ├── BaseExceptionGroup
 ├── GeneratorExit
 ├── KeyboardInterrupt
 ├── SystemExit
 └── Exception
      ├── ArithmeticError
      │    ├── FloatingPointError
      │    ├── OverflowError
      │    └── ZeroDivisionError
      ├── AssertionError
      ├── AttributeError
      ├── BufferError
      ├── EOFError
      ├── ExceptionGroup [BaseExceptionGroup]
      ├── ImportError
      │    └── ModuleNotFoundError
      ├── LookupError
      │    ├── IndexError
      │    └── KeyError
      ├── MemoryError
      ├── NameError
      │    └── UnboundLocalError
      ├── OSError
      │    ├── BlockingIOError
      │    ├── ChildProcessError
      │    ├── ConnectionError
      │    │    ├── BrokenPipeError
      │    │    ├── ConnectionAbortedError
      │    │    ├── ConnectionRefusedError
      │    │    └── ConnectionResetError
      │    ├── FileExistsError
      │    ├── FileNotFoundError
      │    ├── InterruptedError
      │    ├── IsADirectoryError
      │    ├── NotADirectoryError
      │    ├── PermissionError
      │    ├── ProcessLookupError
      │    └── TimeoutError
      ├── ReferenceError
      ├── RuntimeError
      │    ├── NotImplementedError
      │    └── RecursionError
      ├── StopAsyncIteration
      ├── StopIteration
      ├── SyntaxError
      │    └── IndentationError
      │         └── TabError
      ├── SystemError
      ├── TypeError
      ├── ValueError
      │    └── UnicodeError
      │         ├── UnicodeDecodeError
      │         ├── UnicodeEncodeError
      │         └── UnicodeTranslateError
      └── Warning
           ├── BytesWarning
           ├── DeprecationWarning
           ├── EncodingWarning
           ├── FutureWarning
           ├── ImportWarning
           ├── PendingDeprecationWarning
           ├── ResourceWarning
           ├── RuntimeWarning
           ├── SyntaxWarning
           ├── UnicodeWarning
           └── UserWarning

Làm thế nào để Python xử lý Oserror?

Xử lý lỗi Oserrors thường có thể được ngăn chặn bằng cách đảm bảo sự tồn tại của các tệp và thư mục đang được truy cập. Oserror, giống như tất cả các lỗi khác, có thể được xử lý bằng cách sử dụng các khối Except. Đây là một ví dụ. Nhập hệ điều hành thử: File1 = Open ("C: \\ Users \ Yashesvinee \ Documents \ MyFolder \ Newfile.using try-except blocks. Here's an example. import os try: file1 = open("C:\\Users\Yashesvinee\Documents\myfolder\newfile.

Điều gì gây ra một giá trịError trong Python?

1. Python valueError là gì? Python valueError được nêu ra khi một hàm nhận được một đối số thuộc loại chính xác nhưng giá trị không phù hợp. Ngoài ra, tình hình không nên được mô tả bởi một ngoại lệ chính xác hơn như IndexError.when a function receives an argument of the correct type but an inappropriate value. Also, the situation should not be described by a more precise exception such as IndexError.

Làm cách nào để sửa lỗi tên của tên trong Python?

Python "NameError: Tên không được xác định" xảy ra khi chúng ta cố gắng truy cập một biến hoặc hàm không được xác định hoặc trước khi nó được xác định.Để giải quyết lỗi, hãy đảm bảo bạn chưa viết sai tên của biến và truy cập nó sau khi nó được khai báo.make sure you haven't misspelled the variable's name and access it after it has been declared.

3 loại lỗi trong Python là gì?

Chủ yếu có ba loại lỗi có thể phân biệt trong Python: lỗi cú pháp, ngoại lệ và lỗi logic.syntax errors, exceptions and logical errors.