Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1 | .. currentmodule:: asyncio |
| 2 | |
| 3 | |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 4 | .. _asyncio-exceptions: |
| 5 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 6 | ========== |
| 7 | Exceptions |
| 8 | ========== |
| 9 | |
| 10 | |
| 11 | .. exception:: TimeoutError |
| 12 | |
| 13 | The operation has exceeded the given deadline. |
| 14 | |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 15 | .. important:: |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 16 | This exception is different from the builtin :exc:`TimeoutError` |
| 17 | exception. |
| 18 | |
| 19 | |
| 20 | .. exception:: CancelledError |
| 21 | |
| 22 | The operation has been cancelled. |
| 23 | |
Carol Willing | a3c8ba7 | 2018-09-13 16:14:41 -0700 | [diff] [blame] | 24 | This exception can be caught to perform custom operations |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 25 | when asyncio Tasks are cancelled. In almost all situations the |
| 26 | exception must always be re-raised. |
| 27 | |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 28 | .. important:: |
Carol Willing | a3c8ba7 | 2018-09-13 16:14:41 -0700 | [diff] [blame] | 29 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 30 | This exception is a subclass of :exc:`Exception`, so it can be |
| 31 | accidentally suppressed by ``try..except`` block:: |
| 32 | |
| 33 | try: |
| 34 | await operation |
| 35 | except Exception: |
| 36 | # The cancellation is broken because the *except* block |
| 37 | # suppresses the CancelledError exception. |
| 38 | log.log('an error has occurred') |
| 39 | |
| 40 | Instead, the following pattern should be used:: |
| 41 | |
| 42 | try: |
| 43 | await operation |
| 44 | except asyncio.CancelledError: |
| 45 | raise |
| 46 | except Exception: |
| 47 | log.log('an error has occurred') |
| 48 | |
| 49 | |
| 50 | .. exception:: InvalidStateError |
| 51 | |
| 52 | Invalid internal state of :class:`Task` or :class:`Future`. |
| 53 | |
| 54 | Can be raised in situations like setting a result value for a |
| 55 | *Future* object that already has a result value set. |
| 56 | |
| 57 | |
| 58 | .. exception:: SendfileNotAvailableError |
| 59 | |
Carol Willing | a3c8ba7 | 2018-09-13 16:14:41 -0700 | [diff] [blame] | 60 | The "sendfile" syscall is not available for the given |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 61 | socket or file type. |
| 62 | |
| 63 | A subclass of :exc:`RuntimeError`. |
| 64 | |
| 65 | |
| 66 | .. exception:: IncompleteReadError |
| 67 | |
| 68 | Incomplete read error. |
| 69 | |
| 70 | Raised by :ref:`asyncio streams <asyncio-streams>` APIs. |
| 71 | |
| 72 | This exception is a subclass of :exc:`EOFError`. |
| 73 | |
| 74 | .. attribute:: expected |
| 75 | |
| 76 | Total number (:class:`int`) of expected bytes. |
| 77 | |
| 78 | .. attribute:: partial |
| 79 | |
| 80 | Read :class:`bytes` string before the end of stream was reached. |
| 81 | |
| 82 | |
| 83 | .. exception:: LimitOverrunError |
| 84 | |
| 85 | Reached the buffer limit while looking for a separator. |
| 86 | |
| 87 | Raised by :ref:`asyncio streams <asyncio-streams>` APIs. |
| 88 | |
| 89 | .. attribute:: consumed |
| 90 | |
| 91 | Total number of to be consumed bytes. |