çµã¿èŸŒã¿äŸå€Â¶
Python ã«ãããŠããã¹ãŠã®äŸå€ã¯ BaseException ããæŽŸçããã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ã§ãªããã°ãªããŸãããç¹å®ã®ã¯ã©ã¹ãèšåãã except ç¯ã䌎ã try æã«ãããŠããã®ç¯ã¯ãã®ã¯ã©ã¹ããæŽŸçããäŸå€ã¯ã©ã¹ãåŠçããŸããããã®ã¯ã©ã¹ã®æŽŸç å
ã®äŸå€ã¯ã©ã¹ã¯åŠçããŸããããµãã¯ã©ã¹åã®é¢ä¿ã«ãªã 2 ã€ã®äŸå€ã¯ã©ã¹ã¯ãããããåãååã ã£ãå Žåã§ãçãããªãããŸããã
ãã®ç« ã§æããçµã¿èŸŒã¿äŸå€ã¯ãã€ã³ã¿ããªã¿ãçµã¿èŸŒã¿é¢æ°ã«ãã£ãŠçæãããŸããç¹ã«æ³šèšããªããããããããã¯ãšã©ãŒã®è©³ããåå ã瀺ã "é¢é£å€ (associated value)" ãæã¡ãŸãããã®å€ã¯ãè€æ°ã®æ å ± (ãšã©ãŒã³ãŒããããã®ã³ãŒãã説æããæååãªã©) ã®æååãã¿ãã«ã§ããé¢é£å€ã¯éåžžãäŸå€ã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã«åŒæ°ãšããŠæž¡ãããŸãã
ãŠãŒã¶ã«ããã³ãŒããçµã¿èŸŒã¿äŸå€ãéåºã§ããŸããããã䜿ã£ãŠãäŸå€ãã³ãã©ããã¹ãããããã€ã³ã¿ããªã¿ãåãäŸå€ãéåºããç¶æ³ãš "ã¡ããã©åããããª" ãšã©ãŒæ¡ä»¶ã§ããããšãå ±åãããã§ããŸãããããããŠãŒã¶ã®ã³ãŒããé©åã§ãªããšã©ãŒãéåºããã®ã劚ããæ¹æ³ã¯ãªãã®ã§æ³šæããŠãã ããã
çµã¿èŸŒã¿äŸå€ã¯ã©ã¹ã¯æ°ããªäŸå€ãå®çŸ©ããããã«ãµãã¯ã©ã¹åããããšãã§ããŸããæ°ããäŸå€ã¯ãException ã¯ã©ã¹ããã®ãµãã¯ã©ã¹ã®äžã€ããæŽŸçããããšããå§ãããŸãã BaseException ããã¯æŽŸçããªãã§äžãããäŸå€ãå®çŸ©ããäžã§ã®è©³ããæ
å ±ã¯ã Python ãã¥ãŒããªã¢ã«ã® ãŠãŒã¶ãŒå®çŸ©äŸå€ ã®é
ç®ã«ãããŸãã
äŸå€ã³ã³ããã¹ã¶
äŸå€ãªããžã§ã¯ãã®3ã€ã®å±æ§ã¯ãäŸå€ãéåºãããåžžçšã«é¢ããæ å ±ãæäŸããŸã:
- BaseException.__context__¶
- BaseException.__cause__¶
- BaseException.__suppress_context__¶
When raising a new exception while another exception is already being handled, the new exception's
__context__attribute is automatically set to the handled exception. An exception may be handled when anexceptorfinallyclause, or awithstatement, is used.This implicit exception context can be supplemented with an explicit cause by using
fromwithraise:raise new_exc from original_exc
fromã«ç¶ãåŒã¯äŸå€ãNoneã§ãªããŠã¯ãªããŸããã åŒã¯éåºãããäŸå€ã®__cause__ãšããŠèšå®ãããŸãã__cause__ãèšå®ããããšã¯ã__suppress_context__屿§ãæé»çã«Trueã«èšå®ããããšã«ããªãã®ã§ãraise new_exc from Noneã䜿ãããšã§å¹ççã«å€ãäŸå€ãæ°ãããã®ã§çœ®ãæããŠè¡šç€ºãã (äŸãã°ãKeyErrorãAttributeErrorã«çœ®ãæã)ãå€ãäŸå€ã¯ãããã°æã®èª¿æ»ã§äœ¿ãããã__context__ã«æ®ãããšãã§ããŸããããã©ã«ãã® traceback 衚瀺ã³ãŒãã¯ãäŸå€èªäœã® traceback ã«å ãããããã®é£éãããäŸå€ã衚瀺ããŸãã
__cause__ã§æç€ºçã«é£éãããäŸå€ã¯ãååšãããªãã°åžžã«è¡šç€ºãããŸãã__context__ã§æé»ã«é£éãããäŸå€ã¯ã__cause__ãNoneãã€__suppress_context__ã false ã®å Žåã«ã®ã¿è¡šç€ºãããŸãããããã«ãããé£éãããäŸå€ã«ç¶ããŠããã®äŸå€èªäœã¯åžžã«è¡šç€ºãããŸãããã®ãããtraceback ã®æçµè¡ã«ã¯ãåžžã«éåºãããæåŸã®äŸå€ã衚瀺ãããŸãã
çµã¿èŸŒã¿äŸå€ããç¶æ¿ãã¶
User code can create subclasses that inherit from an exception type.
It's recommended to only subclass one exception type at a time to avoid
any possible conflicts between how the bases handle the args
attribute, as well as due to possible memory layout incompatibilities.
CPython å®è£ ã®è©³çް: 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.
åºåºã¯ã©ã¹Â¶
以äžã®äŸå€ã¯ãäž»ã«ä»ã®äŸå€ã®åºåºã¯ã©ã¹ãšããŠäœ¿ãããŸãã
- exception BaseException¶
å šãŠã®çµã¿èŸŒã¿äŸå€ã®åºåºã¯ã©ã¹ã§ãããŠãŒã¶å®çŸ©ã®äŸå€ã«çŽæ¥ç¶æ¿ãããããšã¯æå³ãããŠããŸãã (ç¶æ¿ã«ã¯
Exceptionã䜿ã£ãŠãã ãã)ããã®ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ã«str()ãåŒã°ããå Žåãã€ã³ã¹ã¿ã³ã¹ãžã®åŒæ°ã®è¡šçŸããåŒæ°ãç¡ãå Žåã«ã¯ç©ºæååãè¿ãããŸãã- args¶
äŸå€ã³ã³ã¹ãã©ã¯ã¿ã«äžããããåŒæ°ã®ã¿ãã«ã§ããçµã¿èŸŒã¿äŸå€ã¯æ®éããšã©ãŒã¡ãã»ãŒãžãäžããäžã€ã®æååã ããåŒæ°ãšããŠåŒã°ããŸãããäžã«ã¯ (
OSErrorãªã©) ããã€ãã®åŒæ°ãå¿ èŠãšãããã®ã¿ãã«ã®èŠçŽ ã«ç¹å¥ãªæå³ã蟌ãããã®ããããŸãã
- with_traceback(tb)¶
This method sets tb as the new traceback for the exception and returns the exception object. It was more commonly used before the exception chaining features of PEP 3134 became available. The following example shows how we can convert an instance of
SomeExceptioninto an instance ofOtherExceptionwhile preserving the traceback. Once raised, the current frame is pushed onto the traceback of theOtherException, as would have happened to the traceback of the originalSomeExceptionhad we allowed it to propagate to the caller.try: ... except SomeException: tb = sys.exception().__traceback__ raise OtherException(...).with_traceback(tb)
- __traceback__¶
A writable field that holds the traceback object associated with this exception. See also: raise æ.
- add_note(note)¶
äŸå€ã®ããŒããšããŠæåå
noteã远å ããŸããããŒãã¯æšæºã®ãã¬ãŒã¹ããã¯ã§äŸå€æååã®åŸã«è¡šç€ºãããŸããnoteãæåå以å€ã®å ŽåTypeErrorãéåºãããŸããAdded in version 3.11.
- __notes__¶
add_note()ã§è¿œå ãããäŸå€ã®ããŒãã®ãªã¹ãããã®å±æ§ã¯add_note()ãåŒã³åºããšçæãããŸããAdded in version 3.11.
- exception Exception¶
ã·ã¹ãã çµäºä»¥å€ã®å šãŠã®çµã¿èŸŒã¿äŸå€ã¯ãã®ã¯ã©ã¹ããæŽŸçããŠããŸããå šãŠã®ãŠãŒã¶å®çŸ©äŸå€ããã®ã¯ã©ã¹ããæŽŸçãããã¹ãã§ãã
- exception ArithmeticError¶
ç®è¡äžã®æ§ã ãªãšã©ãŒã«å¯ŸããŠéåºãããçµã¿èŸŒã¿äŸå€
OverflowError,ZeroDivisionError,FloatingPointErrorã®åºåºã¯ã©ã¹ã§ãã
- exception BufferError¶
ãããã¡ ã«é¢é£ããæäœãè¡ããªãã£ããšãã«éåºãããŸãã
- exception LookupError¶
ãããã³ã°ãŸãã¯ã·ãŒã±ã³ã¹ã§äœ¿ãããããŒãã€ã³ãã¯ã¹ãç¡å¹ãªå Žåã«éåºãããäŸå€
IndexErrorããã³KeyErrorã®åºåºã¯ã©ã¹ã§ããcodecs.lookup()ã«ãã£ãŠçŽæ¥éåºãããããšããããŸãã
å ·è±¡äŸå€Â¶
以äžã®äŸå€ã¯ãéåžžéåºãããäŸå€ã§ãã
- exception AttributeError¶
屿§åç § (屿§åç § ãåç §) ãä»£å ¥ã倱æããå Žåã«éåºãããŸã (ãªããžã§ã¯ãã屿§ã®åç §ã屿§ã®ä»£å ¥ããŸã£ãããµããŒãããŠããªãå Žåã«ã¯
TypeErrorãéåºãããŸã)ãThe optional name and obj keyword-only arguments set the corresponding attributes:
- name¶
The name of the attribute that was attempted to be accessed.
- obj¶
The object that was accessed for the named attribute.
- exception EOFError¶
Raised when the
input()function hits an end-of-file condition (EOF) without reading any data. (Note: theio.TextIOBase.read()andio.IOBase.readline()methods return an empty string when they hit EOF.)
- exception FloatingPointError¶
çŸåšã¯äœ¿ãããŠããŸããã
- exception GeneratorExit¶
ãžã§ãã¬ãŒã¿ ã ã³ã«ãŒãã³ ãéãããããšãã«éåºãããŸãã
generator.close()ãšcoroutine.close()ãåç §ããŠãã ããããã®äŸå€ã¯å³å¯ã«èšãã°ãšã©ãŒã§ã¯ãªãã®ã§ãExceptionã§ã¯ãªãBaseExceptionãçŽæ¥ç¶æ¿ããŠããŸãã
- exception ImportError¶
importæã§ã¢ãžã¥ãŒã«ãããŒãããããšããŠåé¡ãçºçãããšéåºãããŸããfrom ... importã®äžã®"from list" (蚳泚ïŒ...ã®éšå)ã®ååãèŠã€ãããªããšãã«ãéåºãããŸãããªãã·ã§ã³ã®ããŒã¯ãŒãå°çšåŒæ° name ãš path ã¯ã察å¿ãã屿§ã«èšå®ãããŸã:
- name¶
ã€ã³ããŒãã詊ã¿ãã¢ãžã¥ãŒã«ã®ååã
- path¶
äŸå€ãåŒãèµ·ããããã¡ã€ã«ã®ãã¹ã
- exception ModuleNotFoundError¶
ImportErrorã®ãµãã¯ã©ã¹ã§ãimportæã§ã¢ãžã¥ãŒã«ãèŠã€ãããªãå Žåã«éåºãããŸãããŸããsys.modulesã«Noneãå«ãŸããå Žåã«ãéåºãããŸããAdded in version 3.6.
- exception IndexError¶
ã·ãŒã±ã³ã¹ã®æ·»åãç¯å²å€ã®å Žåã«éåºãããŸãã (ã¹ã©ã€ã¹ã®ã€ã³ãã¯ã¹ã¯ã·ãŒã±ã³ã¹ã®ç¯å²ã«åãŸãããã«æé»ã®ãã¡ã«èª¿æŽãããŸã; ã€ã³ãã¯ã¹ãæŽæ°ã§ãªãå Žåã
TypeErrorãéåºãããŸãã)
- exception KeyError¶
ãããã³ã° (èŸæž) ã®ããŒããæ¢åã®ããŒã®éåå ã«èŠã€ãããªãã£ãå Žåã«éåºãããŸãã
- exception KeyboardInterrupt¶
ãŠãŒã¶ãå²ã蟌ã¿ã㌠(é垞㯠Control-C ãŸã㯠Delete) ãæŒããå Žåã«éåºãããŸããå®è¡äžãå²ã蟌ã¿ã¯å®æçã«ç£èŠãããŸãã
Exceptionãææããã³ãŒãã«èª€ã£ãŠææãããŠã€ã³ã¿ããªã¿ã®çµäºãé»å®³ãããªãããã«ããã®äŸå€ã¯BaseExceptionãç¶æ¿ããŠããŸããæ³šé
Catching a
KeyboardInterruptrequires special consideration. Because it can be raised at unpredictable points, it may, in some circumstances, leave the running program in an inconsistent state. It is generally best to allowKeyboardInterruptto end the program as quickly as possible or avoid raising it entirely. (See Note on Signal Handlers and Exceptions.)
- exception MemoryError¶
ããæäœäžã«ã¡ã¢ãªãäžè¶³ãããããã®ç¶æ³ã¯ (ãªããžã§ã¯ããããã€ãæ¶å»ããããšã§) ãŸã 埩æ§å¯èœãããããªãå Žåã«éåºãããŸãããã®äŸå€ã®é¢é£å€ã¯ãã¡ã¢ãªäžè¶³ã«ãªã£ã (å éš) æäœã®çš®é¡ã瀺ãæååã§ããäžå±€ã®ã¡ã¢ãªç®¡çã¢ãŒããã¯ã㣠(C ã®
malloc()颿°) ã®ããã«ãã€ã³ã¿ããªã¿ãçŸç¶ããå®ç§ã«åŸ©æ§ã§ãããšã¯ããããªãã®ã§æ³šæããŠãã ãããããã§ããããã°ã©ã ã®æŽèµ°ãåå ã®å Žåã«åããŠå®è¡ã¹ã¿ãã¯ã®ãã¬ãŒã¹ããã¯ãåºåã§ããããã«ãäŸå€ãéåºãããŸãã
- exception NameError¶
ããŒã«ã«ãŸãã¯ã°ããŒãã«ã®ååãèŠã€ãããªãã£ãå Žåã«éåºãããŸããããã¯é修食㮠(蚳泚:
spam.eggã§ã¯ãªãåã«eggã®ãããª) ååã®ã¿ã«é©çšãããŸããé¢é£å€ã¯èŠã€ãããªãã£ãååãå«ããšã©ãŒã¡ãã»ãŒãžã§ããThe optional name keyword-only argument sets the attribute:
- name¶
The name of the variable that was attempted to be accessed.
ããŒãžã§ã³ 3.10 ã§å€æŽ:
name屿§ã远å ãããŸããã
- exception NotImplementedError¶
ãã®äŸå€ã¯
RuntimeErrorããæŽŸçããŠããŸãããŠãŒã¶å®çŸ©ã®åºåºã¯ã©ã¹ã«ãããŠãæœè±¡ã¡ãœãããæŽŸçã¯ã©ã¹ã§ãªãŒãã©ã€ããããããšãèŠæ±ããå Žåã«ãã®äŸå€ãéåºããªããŠã¯ãªããŸããããŸãã¯ã¯ã©ã¹ã¯å®è£ äžã§ããæ¬æ¥ã®å®è£ ã远å ããå¿ èŠãããããšã瀺ããŸããæ³šé
æŒç®åãã¡ãœããããµããŒããããŠããªãããšã瀺ãç®çã§ãã®äŸå€ã䜿çšããã¹ãã§ã¯ãããŸããããã®ãããªã±ãŒã¹ã§ã¯ãªãã¬ãŒã¿ãã¡ãœãããæªå®çŸ©ã®ãŸãŸãšãããããµãã¯ã©ã¹ã®å Žåã¯
Noneãèšå®ããŠãã ãããæ³šæ
NotImplementedErrorandNotImplementedare not interchangeable. This exception should only be used as described above; seeNotImplementedfor details on correct usage of the built-in constant.
- exception OSError([arg])¶
- exception OSError(errno, strerror[, filename[, winerror[, filename2]]])
ãã®äŸå€ã¯ã·ã¹ãã 颿°ãã·ã¹ãã é¢é£ã®ãšã©ãŒãè¿ããå Žåã«éåºãããŸããäŸãã° "file not found" ã "disk full" ã®ãã㪠I/O ã®å€±æãçºçãããšãã§ã (åŒæ°ã®åãäžæ£ãªå Žåããä»ã®å¶çºçãªãšã©ãŒã¯é€ããŸã)ã
ã³ã³ã¹ãã©ã¯ã¿ã®2çªç®ã®åœ¢åŒã¯äžèšã®å¯Ÿå¿ãã屿§ãèšå®ããŸããæå®ãããªãã£ãå Žå屿§ã¯ããã©ã«ãã§
Noneã§ããåŸæ¹äºææ§ã®ããã«ãåŒæ°ã3ã€æž¡ãããå Žåãargs屿§ã¯æåã®2ã€ã®èŠçŽ ã®ã¿ãããªãã¿ãã«ãæã¡ãŸããã³ã³ã¹ãã©ã¯ã¿ã¯å®éã«ã¯ã OS exceptions ã§è¿°ã¹ãããŠãã
OSErrorã®ãµãã¯ã©ã¹ãè¿ãããšããããããŸããç¹å®ã®ãµãã¯ã©ã¹ã¯æçµçãªerrnoå€ã«ãããŸãããã®æåã¯OSErrorãçŽæ¥ãŸãã¯ãšã€ãªã¢ã¹ã§æ§ç¯ãããµãã¯ã©ã¹åæã«ç¶æ¿ãããªãã£ãå Žåã«ã®ã¿çºçããŸãã- errno¶
C 倿°
errnoã«ç±æ¥ããæ°å€ãšã©ãŒã³ãŒãã§ãã
- winerror¶
Windows ã«ãããŠããã€ãã£ã Windows ãšã©ãŒã³ãŒããäžããŸãããããŠ
errno屿§ã¯ POSIX ã§ãããã€ãã£ããšã©ãŒã³ãŒããžã®ãããã®ç¿»èš³ã§ããWindows ã§ã¯ãwinerror ã³ã³ã¹ãã©ã¯ã¿åŒæ°ãæŽæ°ã®å Žå
errno屿§ã¯ Windows ãšã©ãŒã³ãŒãããæ±ºå®ãããerrno åŒæ°ã¯ç¡èŠãããŸããä»ã®ãã©ãããã©ãŒã ã§ã¯ winerror åŒæ°ã¯ç¡èŠãããwinerror屿§ã¯ååšããŸããã
- strerror¶
The corresponding error message, as provided by the operating system. It is formatted by the C functions
perror()under POSIX, andFormatMessage()under Windows.
- filename¶
- filename2¶
ãã¡ã€ã«ã·ã¹ãã ãã¹ã1ã€é¢äžããäŸå€ (äŸãã°
open()ãos.unlink()) ã®å Žåãfilenameã¯é¢æ°ã«æž¡ããããã¡ã€ã«åã§ãã ãã¡ã€ã«ã·ã¹ãã ãã¹ã2ã€é¢äžãã颿° (äŸãã°os.rename()) ã®å Žåãfilename2ã¯é¢æ°ã«æž¡ããã2ã€ç®ã®ãã¡ã€ã«åã§ãã
ããŒãžã§ã³ 3.3 ã§å€æŽ:
EnvironmentError,IOError,WindowsError,socket.error,select.errorandmmap.errorhave been merged intoOSError, and the constructor may return a subclass.ããŒãžã§ã³ 3.4 ã§å€æŽ:
filename屿§ã filesystem encoding and error handler ã®ãšã³ã³ãŒãã£ã³ã°ã§ãšã³ã³ãŒãããã³ãŒããããååããã颿°ã«æž¡ãããå ã ã®ãã¡ã€ã«åã«ãªããŸããã ãŸããfilename2 ã³ã³ã¹ãã©ã¯ã¿åŒæ°ã远å ãããŸããã
- exception OverflowError¶
ç®è¡æŒç®ã®çµæã衚çŸã§ããªã倧ããªå€ã«ãªã£ãå Žåã«éåºãããŸããããã¯æŽæ°ã§ã¯èµ·ãããŸãã (ããã
MemoryErrorãéåºãããããšã«ãªãã§ããã)ãããããæŽå²çãªçç±ã®ãããèŠæ±ãããç¯å²ã®å€ã®æŽæ°ã«å¯Ÿã㊠OverflowError ãéåºãããããšããããŸããC ã®æµ®åå°æ°ç¹æŒç®ã®äŸå€åŠçã¯æšæºåãããŠããªãã®ã§ãã»ãšãã©ã®æµ®åå°æ°ç¹æŒç®ããã§ãã¯ãããŸããã
- exception PythonFinalizationError¶
This exception is derived from
RuntimeError. It is raised when an operation is blocked during interpreter shutdown also known as Python finalization.Examples of operations which can be blocked with a
PythonFinalizationErrorduring the Python finalization:sys.is_finalizing()颿°ãåç §ããŠãã ãããAdded in version 3.13: 以åã¯
RuntimeErrorããã®ãŸãŸéåºããŠããŸãããããŒãžã§ã³ 3.14 ã§å€æŽ:
threading.Thread.join()can now raise this exception.
- exception RecursionError¶
ãã®äŸå€ã¯
RuntimeErrorãç¶æ¿ããŠããŸããã€ã³ã¿ãŒããªã¿ãæå€§å垰深床 (sys.getrecursionlimit()ãåç §) ã®è¶ éãæ€åºãããšéåºãããŸããAdded in version 3.5: 以åã¯
RuntimeErrorããã®ãŸãŸéåºããŠããŸããã
- exception ReferenceError¶
weakref.proxy()ã«ãã£ãŠçæããã匱åç § (weak reference) ãããã·ã䜿ã£ãŠãã¬ãŒããžã³ã¬ã¯ã·ã§ã³ã«ãã£ãŠååãããåŸã®åç §å¯Ÿè±¡ãªããžã§ã¯ãã®å±æ§ã«ã¢ã¯ã»ã¹ããå Žåã«éåºãããŸãã匱åç §ã«ã€ããŠã¯weakrefã¢ãžã¥ãŒã«ãåç §ããŠãã ããã
- exception RuntimeError¶
ä»ã®ã«ããŽãªã«åé¡ã§ããªããšã©ãŒãæ€åºãããå Žåã«éåºãããŸããé¢é£å€ã¯ãäœãåé¡ã ã£ãã®ãããã詳现ã«ç€ºãæååã§ãã
- exception StopIteration¶
çµèŸŒã¿é¢æ°
next()ãš iterator ã®__next__()ã¡ãœããã«ãã£ãŠããã®ã€ãã¬ãŒã¿ãçæããã¢ã€ãã ããã以äžãªãããšãäŒããããã«éåºãããŸãã- value¶
ãã®äŸå€ãªããžã§ã¯ãã«ã¯äžã€ã®å±æ§
valueããããäŸå€ãæ§æããéã«åŒæ°ãšããŠäžããããããã©ã«ãã¯Noneã§ãã
generator ã coroutine 颿°ãè¿ããšããæ°ãã
StopIterationã€ã³ã¹ã¿ã³ã¹ãéåºãããŸãã 颿°ã®è¿ãå€ã¯äŸå€ã®ã³ã³ã¹ãã©ã¯ã¿ã®valueåŒæ°ãšããŠäœ¿ãããŸãããžã§ãã¬ãŒã¿ã®ã³ãŒããçŽæ¥çãããã¯éæ¥çã«
StopIterationãéåºããå Žåã¯ãRuntimeErrorã«å€æãããŸã (StopIterationã¯å€æåŸã®äŸå€ã®åå ãšããŠä¿æãããŸã)ãããŒãžã§ã³ 3.3 ã§å€æŽ:
value屿§ãšãžã§ãã¬ãŒã¿é¢æ°ãå€ãè¿ãããã«ãããäœ¿ãæ©èœã远å ãããŸãããããŒãžã§ã³ 3.5 ã§å€æŽ:
from __future__ import generator_stopã«ãã RuntimeError ãžã®å€æãå°å ¥ãããŸããã PEP 479 ãåç §ããŠãã ãããããŒãžã§ã³ 3.7 ã§å€æŽ: PEP 479 ãå šãŠã®ã³ãŒãã§ããã©ã«ãã§æå¹åãããŸãã: ãžã§ãã¬ãŒã¿ããéåºããã
StopIterationã¯RuntimeErrorã«å€æãããŸãã
- exception StopAsyncIteration¶
ã€ãã¬ãŒã·ã§ã³ã忢ããããã«ã asynchronous iterator ãªããžã§ã¯ãã®
__anext__()ã¡ãœããã«ãã£ãŠè¿ãããå¿ èŠããããŸããAdded in version 3.5.
- exception SyntaxError(message, details)¶
ããŒã¶ãæ§æãšã©ãŒã«ééããå Žåã«éåºãããŸãããã®äŸå€ã¯
importæãçµã¿èŸŒã¿é¢æ°compile()ãexec()ãeval()ãåæåã¹ã¯ãªããã®èªã¿èŸŒã¿ãæšæºå ¥åã§ (察話çãªå®è¡æã«ã) èµ·ããå¯èœæ§ããããŸããäŸå€ã€ã³ã¹ã¿ã³ã¹ã®
str()ã¯ãšã©ãŒã¡ãã»ãŒãžã®ã¿ãè¿ããŸãã詳现ã¯ã¿ãã«ã§ãåã ã®å±æ§ãšããŠãå©çšã§ããŸãã- filename¶
æ§æãšã©ãŒãçºçãããã¡ã€ã«ã®ååã
- lineno¶
ãã¡ã€ã«ã®ãšã©ãŒãçºçããè¡çªå·ã1ããæ°ãã¯ãããããããã¡ã€ã«ã®æåã®è¡ã®
linenoã¯1ã§ãã
- offset¶
è¡ã®ãšã©ãŒãçºçããåçªå·ã1ããæ°ãã¯ããããããè¡ã®æåã®æåã®
offsetã¯1ã§ãã
- text¶
ãšã©ãŒãå«ããœãŒã¹ã³ãŒãã®ããã¹ãã
- end_lineno¶
ãã¡ã€ã«ã®ãšã©ãŒãçºçããæåŸã®è¡çªå·ã1ããæ°ãã¯ãããããããã¡ã€ã«ã®æåã®è¡ã®
linenoã¯1ã§ãã
- end_offset¶
è¡ã®ãšã©ãŒãçºçããæåŸã®åçªå·ã1ããæ°ãã¯ããããããè¡ã®æåã®æåã®
offsetã¯1ã§ãã
For errors in f-string fields, the message is prefixed by "f-string: " and the offsets are offsets in a text constructed from the replacement expression. For example, compiling f'Bad {a b} field' results in this args attribute: ('f-string: ...', ('', 1, 2, '(a b)n', 1, 5)).
ããŒãžã§ã³ 3.10 ã§å€æŽ:
end_linenoããã³end_offset屿§ã远å ãããŸããã
- exception IndentationError¶
æ£ãããªãã€ã³ãã³ãã«é¢ããæ§æãšã©ãŒã®åºåºã¯ã©ã¹ã§ããããã¯
SyntaxErrorã®ãµãã¯ã©ã¹ã§ãã
- exception TabError¶
ã¿ããšã¹ããŒã¹ãäžè²«ããªãæ¹æ³ã§ã€ã³ãã³ãã«äœ¿ã£ãŠãããšãã«éåºãããŸããããã¯
IndentationErrorã®ãµãã¯ã©ã¹ã§ãã
- exception SystemError¶
Raised when the interpreter finds an internal error, but the situation does not look so serious to cause it to abandon all hope. The associated value is a string indicating what went wrong (in low-level terms). In CPython, this could be raised by incorrectly using Python's C API, such as returning a
NULLvalue without an exception set.If you're confident that this exception wasn't your fault, or the fault of a package you're using, you should report this to the author or maintainer of your Python interpreter. Be sure to report the version of the Python interpreter (
sys.version; it is also printed at the start of an interactive Python session), the exact error message (the exception's associated value) and if possible the source of the program that triggered the error.
- exception SystemExit¶
This exception is raised by the
sys.exit()function. It inherits fromBaseExceptioninstead ofExceptionso that it is not accidentally caught by code that catchesException. This allows the exception to properly propagate up and cause the interpreter to exit. When it is not handled, the Python interpreter exits; no stack traceback is printed. The constructor accepts the same optional argument passed tosys.exit(). If the value is an integer, it specifies the system exit status (passed to C'sexit()function); if it isNone, the exit status is zero; if it has another type (such as a string), the object's value is printed and the exit status is one.sys.exit()ã¯ãã¯ãªãŒã³ã¢ããã®ããã®åŠç (tryæã®finallyç¯) ãå®è¡ãããããã«ããããããŸããããã¬ãå¶åŸ¡äžèœã«ãªããªã¹ã¯ãåããã«ã¹ã¯ãªãããå®è¡ã§ããããã«ããããã«äŸå€ã«å€æãããŸããå³åº§ã«çµäºããããšãçã«åŒ·ãå¿ èŠã§ãããšã (äŸãã°ãos.fork()ãåŒãã åŸã®åããã»ã¹å ) ã«ã¯os._exit()颿°ã䜿ãããšãã§ããŸãã- code¶
ã³ã³ã¹ãã©ã¯ã¿ã«æž¡ãããçµäºã¹ããŒã¿ã¹åã¯ãšã©ãŒã¡ãã»ãŒãžã(ããã©ã«ãã¯
None)
- exception TypeError¶
çµã¿èŸŒã¿æŒç®ãŸãã¯é¢æ°ãé©åã§ãªãåã®ãªããžã§ã¯ãã«å¯ŸããŠé©çšãããéã«éåºãããŸããé¢é£å€ã¯åã®äžæŽåã«é¢ããŠè©³çްãè¿°ã¹ãæååã§ãã
ãã®äŸå€ã¯ããã®ãªããžã§ã¯ãã§å®è¡ããããšããæäœããµããŒããããŠãããããã®äºå®ããªãå Žåã«ãŠãŒã¶ãŒã³ãŒãããéåºããããããããŸããããªããžã§ã¯ãã§ãã®æäœããµããŒãããã€ããã ãããŸã å®è£ ãæäŸããŠããªãã®ã§ããã°ãéåºããé©åãªäŸå€ã¯
NotImplementedErrorã§ãã誀ã£ãåã®åŒæ°ãæž¡ãããå Žå㯠(äŸãã°ã
intãæåŸ ãããã®ã«ãlistãæž¡ããã)TypeErrorãšãªãã¹ãã§ãããããã誀ã£ãå€(äŸãã°ãæåŸ ããç¯å²å€ã®æ°)ãåŒæ°ãšããŠæž¡ãããå Žåã¯ãValueErrorãšãªãã¹ãã§ãã
- exception UnboundLocalError¶
颿°ãã¡ãœããå ã®ããŒã«ã«ãªå€æ°ã«å¯ŸããŠåç §ãè¡ã£ããããã®å€æ°ã«ã¯å€ãä»£å ¥ãããŠããªãã£ãå Žåã«éåºãããŸãã
NameErrorã®ãµãã¯ã©ã¹ã§ãã
- exception UnicodeError¶
Unicode ã«é¢ãããšã³ã³ãŒããŸãã¯ãã³ãŒãã®ãšã©ãŒãçºçããéã«éåºãããŸãã
ValueErrorã®ãµãã¯ã©ã¹ã§ããUnicodeErrorã¯ãšã³ã³ãŒããŸãã¯ãã³ãŒãã®ãšã©ãŒã®èª¬æã屿§ãšããŠæã£ãŠããŸããäŸãã°ãerr.object[err.start:err.end]ã¯ãç¡å¹ãªå ¥åã®ãã¡ã³ãŒããã¯ãåŠçã«å€±æããç®æã衚ããŸãã- encoding¶
ãšã©ãŒãéåºãããšã³ã³ãŒãã£ã³ã°ã®ååã§ãã
- reason¶
ãã®ã³ãŒããã¯ãšã©ãŒã説æããæååã§ãã
- object¶
ã³ãŒããã¯ããšã³ã³ãŒããŸãã¯ãã³ãŒãããããšãããªããžã§ã¯ãã§ãã
- exception UnicodeEncodeError¶
Unicode é¢é£ã®ãšã©ãŒããšã³ã³ãŒãäžã«çºçããéã«éåºãããŸãã
UnicodeErrorã®ãµãã¯ã©ã¹ã§ãã
- exception UnicodeDecodeError¶
Unicode é¢é£ã®ãšã©ãŒããã³ãŒãäžã«çºçããéã«éåºãããŸãã
UnicodeErrorã®ãµãã¯ã©ã¹ã§ãã
- exception UnicodeTranslateError¶
Unicode é¢é£ã®ãšã©ãŒã倿äžã«çºçããéã«éåºãããŸãã
UnicodeErrorã®ãµãã¯ã©ã¹ã§ãã
- exception ValueError¶
æŒç®åã颿°ããæ£ããåã ãé©åã§ãªãå€ãæã€åŒæ°ãåãåã£ããšããã
IndexErrorã®ãããªãã詳现ãªäŸå€ã§ã¯èšè¿°ã§ããªãç¶æ³ã§éåºãããŸãã
- exception ZeroDivisionError¶
é€ç®ãå°äœæŒç®ã®ç¬¬äºåŒæ°ã 0 ã§ãã£ãå Žåã«éåºãããŸããé¢é£å€ã¯æååã§ããã®æŒç®ã«ããã被æŒç®åãšæŒç®åã®åã瀺ããŸãã
以äžã®äŸå€ã¯ãéå»ã®ããŒãžã§ã³ãšã®åŸæ¹äºææ§ã®ããã«æ®ãããŠããŸã; Python 3.3 ãããããã㯠OSError ã®ãšã€ãªã¢ã¹ã§ãã
- exception EnvironmentError¶
- exception IOError¶
- exception WindowsError¶
Windows ã§ã®ã¿å©çšã§ããŸãã
OS äŸå€Â¶
以äžã®äŸå€ã¯ OSError ã®ãµãã¯ã©ã¹ã§ãã·ã¹ãã ãšã©ãŒã³ãŒãã«äŸåããŠéåºãããŸãã
- exception BlockingIOError¶
ããæäœãããã³ããããã³ã°æäœã«èšå®ããããªããžã§ã¯ã (äŸãã°ãœã±ãã) ããããã¯ãããã«ãªã£ãå Žåã«éåºãããŸãã
errnoEAGAIN,EALREADY,EWOULDBLOCKããã³EINPROGRESSã«å¯Ÿå¿ããŸããBlockingIOErrorã¯ãOSErrorã®å±æ§ã«å ããŠäžã€ã®å±æ§ãæã¡ãŸã:
- exception ChildProcessError¶
åããã»ã¹ã®æäœã倱æããå Žåã«éåºãããŸãã
errnoECHILDã«å¯Ÿå¿ããŸãã
- exception ConnectionError¶
ã³ãã¯ã·ã§ã³é¢ä¿ã®åé¡ã®åºåºã¯ã©ã¹ã
ãµãã¯ã©ã¹ã¯
BrokenPipeError,ConnectionAbortedError,ConnectionRefusedError,ConnectionResetErrorã§ãã
- exception BrokenPipeError¶
ConnectionErrorã®ãµãã¯ã©ã¹ã§ãããäžæ¹ã®ç«¯ãéãããããã€ãã«æžã蟌ããããšããããæžã蟌ã¿ã®ããã«ã·ã£ããããŠã³ããããœã±ããã«æžã蟌ããããšããå Žåã«çºçããŸããerrnoEPIPEãšESHUTDOWNã«å¯Ÿå¿ããŸãã
- exception ConnectionAbortedError¶
ConnectionErrorã®ãµãã¯ã©ã¹ã§ãæ¥ç¶ã®è©Šè¡ãéä¿¡çžæã«ãã£ãŠäžæãããå Žåã«çºçããŸããerrnoECONNABORTEDã«å¯Ÿå¿ããŸãã
- exception ConnectionRefusedError¶
ConnectionErrorã®ãµãã¯ã©ã¹ã§ãæ¥ç¶ã®è©Šè¡ãéä¿¡çžæã«ãã£ãŠæåŠãããå Žåã«çºçããŸããerrnoECONNREFUSEDã«å¯Ÿå¿ããŸãã
- exception ConnectionResetError¶
ConnectionErrorã®ãµãã¯ã©ã¹ã§ãæ¥ç¶ãéä¿¡çžæã«ãã£ãŠãªã»ãããããå Žåã«çºçããŸããerrnoECONNRESETã«å¯Ÿå¿ããŸãã
- exception FileExistsError¶
ãã§ã«ååšãããã¡ã€ã«ããã£ã¬ã¯ããªãäœæããããšããå Žåã«éåºãããŸãã
errnoEEXISTã«å¯Ÿå¿ããŸãã
- exception FileNotFoundError¶
èŠæ±ããããã¡ã€ã«ããã£ã¬ã¯ããªãååšããªãå Žåã«éåºãããŸãã
errnoENOENTã«å¯Ÿå¿ããŸãã
- exception InterruptedError¶
ã·ã¹ãã ã³ãŒã«ãå ¥åä¿¡å·ã«ãã£ãŠäžæãããå Žåã«éåºãããŸãã
errnoEINTRã«å¯Ÿå¿ããŸããããŒãžã§ã³ 3.5 ã§å€æŽ: ã·ã°ãã«ãã³ãã©ãäŸå€ãéåºãããã·ã¹ãã ã³ãŒã«ãä¿¡å·ã§äžæãããå Žå Python ã¯
InterruptedErrorãéåºãã代ããã«ã·ã¹ãã ã³ãŒã«ãå詊è¡ããããã«ãªããŸãã (è«æ ã«ã€ããŠã¯ PEP 475 ãåç §ããŠãã ãã) ã
- exception IsADirectoryError¶
ãã£ã¬ã¯ããªã« (
os.remove()ãªã©ã®) ãã¡ã€ã«æäœãèŠæ±ãããå Žåã«éåºãããŸããerrnoEISDIRã«å¯Ÿå¿ããŸãã
- exception NotADirectoryError¶
ãã£ã¬ã¯ããªãžã®æäœ(ããšãã°
os.listdir())ããã£ã¬ã¯ããªä»¥å€ã«å¯ŸããŠèŠæ±ãããå Žåã«éåºãããŸããå€ãã®POSIXãã©ãããã©ãŒã ã§ã¯ãã£ã¬ã¯ããªã§ã¯ãªããã¡ã€ã«ããã£ã¬ã¯ããªãšããŠéãããç§»åãããšãã«ãçºçããå ŽåããããŸããerrnoENOTDIRã«å¯Ÿå¿ããŸãã
- exception PermissionError¶
ååãªã¢ã¯ã»ã¹æš©ãäŸãã°ãã¡ã€ã«ã·ã¹ãã æš©éã®ãªãæäœã詊ã¿ãããå Žåã«éåºãããŸãã
errnoEACCESãEPERMããã³ENOTCAPABLEã«å¯Ÿå¿ããŸããããŒãžã§ã³ 3.11.1 ã§å€æŽ: WASIã®
ENOTCAPABLEã¯PermissionErrorã«ããããããŸããã
- exception ProcessLookupError¶
äžããããããã»ã¹ãååšããªãå Žåã«éåºãããŸãã
errnoESRCHã«å¯Ÿå¿ããŸãã
- exception TimeoutError¶
ã·ã¹ãã 颿°ãã·ã¹ãã ã¬ãã«ã§ã¿ã€ã ã¢ãŠãããå Žåã«éåºãããŸãã
errnoETIMEDOUTã«å¯Ÿå¿ããŸãã
Added in version 3.3: äžèšã®ãã¹ãŠã® OSError ãµãã¯ã©ã¹ã远å ãããŸããã
åè
PEP 3151 - OS ããã³ IO äŸå€éå±€ã®æçŽã
èŠå¶
次ã®äŸå€ã¯èŠåã«ããŽãªãšããŠäœ¿ãããŸãã詳现ã«ã€ããŠã¯ èŠåã«ããŽãª ã®ããã¥ã¡ã³ããåç §ããŠãã ããã
- exception Warning¶
èŠåã«ããŽãªã®åºåºã¯ã©ã¹ã§ãã
- exception UserWarning¶
ãŠãŒã¶ã³ãŒãã«ãã£ãŠçæãããèŠåã®åºåºã¯ã©ã¹ã§ãã
- exception DeprecationWarning¶
ä»ã® Python éçºè ãžåããŠèŠåãçºãããšãã®ã廿¢äºå®ã®æ©èœã«ã€ããŠã®èŠåã®åºåºã¯ã©ã¹ã§ãã
__main__ã¢ãžã¥ãŒã«ãé€ããŠ(PEP 565)ãããã©ã«ãã®èŠåãã£ã«ã¿ãŒã§ç¡å¹åãããŠããŸããPython Development Mode ãæå¹ã«ãããšãã®èŠåã衚瀺ãããŸããThe deprecation policy is described in PEP 387.
- exception PendingDeprecationWarning¶
å€ããªã£ãŠå°æ¥çã«å»æ¢ãããäºå®ã ããä»ã®ãšããã¯å»æ¢ãããŠããªãæ©èœã«ã€ããŠã®èŠåã®åºåºã¯ã©ã¹ã§ãã
è¿ã èµ·ããå¯èœæ§ã®ããæ©èœå»æ¢ã«ã€ããŠèŠåãçºããããšã¯ãŸããªã®ã§ããã®ã¯ã©ã¹ã¯ãã£ãã«äœ¿ããããæ¢ã«æ±ºãŸã£ãŠãã廿¢ã«ã€ããŠã¯
DeprecationWarningãæãŸããã§ããããã©ã«ãã®èŠåãã£ã«ã¿ãŒã§ç¡å¹åãããŠããŸããPython Development Mode ãæå¹ã«ãããšãã®èŠåã衚瀺ãããŸãã
The deprecation policy is described in PEP 387.
- exception SyntaxWarning¶
ææ§ãªæ§æã«å¯ŸããèŠåã®åºåºã¯ã©ã¹ã§ãã
This warning is typically emitted when compiling Python source code, and usually won't be reported when running already compiled code.
- exception RuntimeWarning¶
ãããŸããªã©ã³ã¿ã€ã æåã«å¯ŸããèŠåã®åºåºã¯ã©ã¹ã§ãã
- exception FutureWarning¶
Python ã§æžãããã¢ããªã±ãŒã·ã§ã³ã®ãšã³ããŠãŒã¶ãŒãžåããŠèŠåãçºãããšãã®ã廿¢äºå®ã®æ©èœã«ã€ããŠã®èŠåã®åºåºã¯ã©ã¹ã§ãã
- exception ImportWarning¶
ã¢ãžã¥ãŒã«ã€ã³ããŒãã®èª€ããšæããããã®ã«å¯ŸããèŠåã®åºåºã¯ã©ã¹ã§ãã
ããã©ã«ãã®èŠåãã£ã«ã¿ãŒã§ç¡å¹åãããŠããŸããPython Development Mode ãæå¹ã«ãããšãã®èŠåã衚瀺ãããŸãã
- exception UnicodeWarning¶
Unicode ã«é¢é£ããèŠåã®åºåºã¯ã©ã¹ã§ãã
- exception EncodingWarning¶
ãšã³ã³ãŒãã£ã³ã°ã«é¢é£ããèŠåã®åºåºã¯ã©ã¹ã§ãã
詳现㯠Opt-in EncodingWarning ãåç §ããŠãã ããã
Added in version 3.10.
- exception ResourceWarning¶
ãªãœãŒã¹ã®äœ¿çšã«é¢é£ããèŠåã®åºåºã¯ã©ã¹ã§ãã
ããã©ã«ãã®èŠåãã£ã«ã¿ãŒã§ç¡å¹åãããŠããŸããPython Development Mode ãæå¹ã«ãããšãã®èŠåã衚瀺ãããŸãã
Added in version 3.2.
äŸå€ã°ã«ãŒã¶
以äžã¯é¢ä¿ããªãè€æ°ã®äŸå€ãéåºããå¿
èŠããããšãã«äœ¿çšããŸããäŸå€ã°ã«ãŒãã¯äŸå€ã®éå±€æ§é ã®äžéšã®ãããä»ã®äŸå€ãšåæ§ except ã§åŠçã§ããŸãããŸããexcept* ã«ãã£ãŠå€å¥ã§ããäŸå€ã°ã«ãŒãã«å«ãŸããŠããäŸå€ã®åã«åºã¥ããŠãµãã°ã«ãŒãã«ãããããŸãã
- exception ExceptionGroup(msg, excs)¶
- exception BaseExceptionGroup(msg, excs)¶
ãã®2ã€ã®äŸå€åã¯äžé£ã®äŸå€
excsãå å«ããŸããmsgåŒæ°ã¯æååã®å¿ èŠããããŸãã2ã€ã®ã¯ã©ã¹ã®ç°ãªãç¹ã¯ãBaseExceptionã¯BaseExceptionGroupãæ¡åŒµããŠä»»æã®äŸå€ãå«ããããŸãããExceptionGroupã¯Exceptionãæ¡åŒµããŠExceptionã®ãµãã¯ã©ã¹ã®ã¿ãå«ããããŸãããã®èšèšã«ããexcept Exceptionã¯ExceptionGroupããã£ããããŸãããBaseExceptionGroupã¯ãã£ããããŸãããBaseExceptionGroupã®ã³ã³ã¹ãã©ã¯ã¿ãŒã¯å«ãŸããäŸå€ããã¹ãŠExceptionã®å Žåã¯BaseExceptionGroupã§ã¯ãªãExceptionGroupãè¿ãããã«èªåçã«éžæãããŸããäžæ¹ExceptionGroupã³ã³ã¹ãã©ã¯ã¿ã¯ãExceptionãµãã¯ã©ã¹ä»¥å€ã®äŸå€ãå«ãå Žåã¯TypeErrorãéåºããŸããException groups are generic over the type of their contained exceptions.
CPython å®è£ ã®è©³çް: The
excsparameter may be any sequence, but lists and tuples are specifically processed more efficiently here. For optimal performance, pass a tuple asexcs.- message¶
ã³ã³ã¹ãã©ã¯ã¿ã®
msgåŒæ°ããã®å±æ§ã¯èªã¿èŸŒã¿å°çšã§ãã
- exceptions¶
ã³ã³ã¹ãã©ã¯ã¿ã«æž¡ãããäžé£ã®
excsã«å«ãŸããäŸå€ã®ã¿ãã«ã§ãããã®å±æ§ã¯èªã¿èŸŒã¿å°çšã§ãã
- subgroup(condition)¶
çŸåšã®ã°ã«ãŒãã§ æ¡ä»¶ ã«ããããã瀌åã®ã¿ãå«ãäŸå€ã°ã«ãŒããè¿ããŸããçµæã空ã®å Žåã¯
Noneãè¿ããŸããThe condition can be an exception type or tuple of exception types, in which case each exception is checked for a match using the same check that is used in an
exceptclause. The condition can also be a callable (other than a type object) that accepts an exception as its single argument and returns true for the exceptions that should be in the subgroup.The nesting structure of the current exception is preserved in the result, as are the values of its
message,__traceback__,__cause__,__context__and__notes__fields. Empty nested groups are omitted from the result.The condition is checked for all exceptions in the nested exception group, including the top-level and any nested exception groups. If the condition is true for such an exception group, it is included in the result in full.
Added in version 3.13:
conditioncan be any callable which is not a type object.
- split(condition)¶
subgroup()ãšäŒŒãŠãŸãã(match, rest)ã®ãã¢ãè¿ããŸããmatchã¯subgroup(condition)ã§restã¯æ®ãã®ãããããªãéšåã§ãã
- derive(excs)¶
åã
messageã®äŸå€ã°ã«ãŒããè¿ããŸãããexcsã®äŸå€ãå«ãã§ããŸããThis method is used by
subgroup()andsplit(), which are used in various contexts to break up an exception group. A subclass needs to override it in order to makesubgroup()andsplit()return instances of the subclass rather thanExceptionGroup.subgroup()ãšsplit()ã¯__traceback__ã__cause__ã__context__ãš__notes__ãã£ãŒã«ããå ã®äŸå€ã°ã«ãŒãããderive()ãè¿ãäŸå€ã°ã«ãŒãã«ã³ããŒãããããderive()ã§ã¯ãããã®ãã£ãŒã«ããæŽæ°ããå¿ èŠããããŸããã>>> class MyGroup(ExceptionGroup): ... def derive(self, excs): ... return MyGroup(self.message, excs) ... >>> e = MyGroup("eg", [ValueError(1), TypeError(2)]) >>> e.add_note("a note") >>> e.__context__ = Exception("context") >>> e.__cause__ = Exception("cause") >>> try: ... raise e ... except Exception as e: ... exc = e ... >>> match, rest = exc.split(ValueError) >>> exc, exc.__context__, exc.__cause__, exc.__notes__ (MyGroup('eg', [ValueError(1), TypeError(2)]), Exception('context'), Exception('cause'), ['a note']) >>> match, match.__context__, match.__cause__, match.__notes__ (MyGroup('eg', [ValueError(1)]), Exception('context'), Exception('cause'), ['a note']) >>> rest, rest.__context__, rest.__cause__, rest.__notes__ (MyGroup('eg', [TypeError(2)]), Exception('context'), Exception('cause'), ['a note']) >>> exc.__traceback__ is match.__traceback__ is rest.__traceback__ True
Note that
BaseExceptionGroupdefines__new__(), so subclasses that need a different constructor signature need to override that rather than__init__(). For example, the following defines an exception group subclass which accepts an exit_code and constructs the group's message from it.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)
Like
ExceptionGroup, any subclass ofBaseExceptionGroupwhich is also a subclass ofExceptioncan only wrap instances ofException.Added in version 3.11.
äŸå€ã®ã¯ã©ã¹é局¶
çµã¿èŸŒã¿äŸå€ã®ã¯ã©ã¹éå±€ã¯ä»¥äžã®ãšããã§ã:
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
â âââ PythonFinalizationError
â âââ 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