ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Python문법 - 예외 try except else finally assert raise
    Search: Python Python 2018. 2. 1. 10:01


    참고

    Python예제: http://infos.tistory.com/1982





    * 예외 접수 기본 구조

    try:

       <예외 발생 예상 구문>

    except <예외 종류>

       '예외 처리'

    except 예외종류 as 예외값

       print('예외 처리'.format(예외값))

       '사용자 정의 클래스 에러도 가능하다. ex: except UserEcp as ue'

    except <예외종류1, 예외종류2>

       '예외 처리'

    except:

    '그외 모든 예외'

    pass #예외 회피

    else:                     #생략 가능

       <예외가 발생하지 않을 때 처리>

    finally:                  #생략 가능    

       <예외와 상관없이 무조건 마지막에 수행>


    * 예외 발생


    raise NotImplementedError

    raise NameError("NameMessage123")


    assert A is B #참이 아닐 경우 예외 발생



    * 많이 쓰이는 except 종류

    ZeroDivisionError: #0으로 나눌 없다.

    IndexError: #인덱스를 벗어났다

    TypeError: #Type이 맞지 않거나. 문자열 연산 불가.


    * 전체 예외 목록과 구조( Exception hierarchy )

    BaseException

     +-- SystemExit

     +-- KeyboardInterrupt

     +-- GeneratorExit

     +-- Exception

          +-- StopIteration

          +-- StopAsyncIteration

          +-- ArithmeticError

          |    +-- FloatingPointError

          |    +-- OverflowError

          |    +-- ZeroDivisionError #0으로 나눌 없다.

          +-- 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 #Type이 맞지 않거나. 문자열 연산 불가.

          +-- ValueError

          |    +-- UnicodeError

          |         +-- UnicodeDecodeError

          |         +-- UnicodeEncodeError

          |         +-- UnicodeTranslateError

          +-- Warning

               +-- DeprecationWarning

               +-- PendingDeprecationWarning

               +-- RuntimeWarning

               +-- SyntaxWarning

               +-- UserWarning

               +-- FutureWarning

               +-- ImportWarning

               +-- UnicodeWarning

               +-- BytesWarning

               +-- ResourceWarningBaseException

     +-- 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



    댓글