행궁동 데이터 엔지니어

반응형

Python으로 코딩을 하다 보면 

try / except 문으로 예외(Exception) 처리를 하는 경우가 많습니다.

except 부분에 Exception을 적어 모든 예외를 처리할 수 있지만, 특정 Case의 Exception만 명시해서 처리할 수도 있습니다.

아래에 특정 Case Exception만 예외처리할 경우 참고할 수 있는 Python Exception 리스트 첨부드립니다.

try / except 문 예시

try:
    ...
except Exception: # 모든 예외 케이스 예외 처리
	...

try:
    ...
except ImportError: # ImportError인 경우에만 예외처리
	...
    
try:
    ...
except Exception as e: # 예외 케이스를 e로 받아서 사용 할 수 있음
	...

 

 

Python에 내장되어 except 부분에 명시해 사용할 수 있는 Exception 리스트

  • In Python, all exceptions must be instances of a class that derives from BaseException.
BaseException
 +-- SystemExit
 +-- KeyboardInterrupt
 +-- GeneratorExit
 +-- Exception
      +-- StopIteration
      +-- StopAsyncIteration
      +-- ArithmeticError
      |    +-- FloatingPointError
      |    +-- OverflowError
      |    +-- ZeroDivisionError
      +-- AssertionError
      +-- AttributeError
      +-- BufferError
      +-- EOFError
      +-- 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
      +-- SyntaxError
      |    +-- IndentationError
      |         +-- TabError
      +-- SystemError
      +-- TypeError
      +-- ValueError
      |    +-- UnicodeError
      |         +-- UnicodeDecodeError
      |         +-- UnicodeEncodeError
      |         +-- UnicodeTranslateError
      +-- Warning
           +-- DeprecationWarning
           +-- PendingDeprecationWarning
           +-- RuntimeWarning
           +-- SyntaxWarning
           +-- UserWarning
           +-- FutureWarning
           +-- ImportWarning
           +-- UnicodeWarning
           +-- BytesWarning
           +-- ResourceWarning

 

참고 자료

1. 우균 님 블로그, [Python Exception 계층], woogyun.tistory.com/649

2. docs.python.org/3/library/exceptions.html#exception-hierarchy

Built-in Exceptions — Python 3.9.4 documentation

Built-in Exceptions In Python, all exceptions must be instances of a class that derives from BaseException. In a try statement with an except clause that mentions a particular class, that clause also handles any exception classes derived from that class (b

docs.python.org

 

 

반응형

이 글을 공유합시다

facebook twitter kakaoTalk kakaostory naver band