Open woneuy01 opened 4 years ago
try: items = ['a', 'b'] third = items[2] print("This won't print") except Exception: print("got an error")
print("continuing")
got an error continuing
The exception code can access a variable that contains information about exactly what the error was. Thus, for example, in the except clause you could print out the information that would normally be printed as an error message but continue on with execution of the rest of the program.
try: items = ['a', 'b'] third = items[2] print("This won't print") except Exception as e: //e 에러나는 이유가 뭔지도 나온다. print("got an error") print(e)
print("continuing")
got an error IndexError: list index out of range on line 3 continuing
students = [('Timmy', 95, 'Will pass'), ('Martha', 70), ('Betty', 82, 'Will pass'), ('Stewart', 50, 'Will not pass'), ('Ashley', 68), ('Natalie', 99, 'Will pass'), ('Archie', 71), ('Carl', 45, 'Will not pass')]
passing = {'Will pass': 0, 'Will not pass': 0} for tup in students: try: if tup[2] == 'Will pass': passing['Will pass'] += 1 elif tup[2] == 'Will not pass': passing['Will not pass'] += 1 except Exception as e: print(e)
패스 여부가 안나온 항목이 3개 IndexError: tuple index out of range on line 7 IndexError: tuple index out of range on line 7 IndexError: tuple index out of range on line 7
nums = [5, 9, '4', 3, 2, 1, 6, 5, '7', 4, 3, 2, 6, 7, 8, '0', 3, 4, 0, 6, 5, '3', 5, 6, 7, 8, '3', '1', 5, 6, 7, 9, 3, 2, 5, 6, '9', 2, 3, 4, 5, 1]
plus_four = []
for num in nums: try: plus_four.append(num+4) except Exception: //숫자대신 '4' string이 들어간 구간은 error로 나온다. plus_four.append('Error')
print(plus_four)
[9, 13, 'Error', 7, 6, 5, 10, 9, 'Error', 8, 7, 6, 10, 11, 12, 'Error', 7, 8, 4, 10, 9, 'Error', 9, 10, 11, 12, 'Error', 'Error', 9, 10, 11, 13, 7, 6, 9, 10, 'Error', 6, 7, 8, 9, 5]
Syntax error : 파이썬이 코딩 자체를 이해 못할때 뭔가 스펠링 에러등을 명령 인식 못함 Runtime error: 파이썬이 코딩은 이해하는데 다른 무언가가 잘못되었을때 Semantic error: 파이썬이 코딩을 실행했는데 내가 원하는 것이 아닐때 ( 코드에 에러는 안나지면 결과가 내가 원하는 것이 아님)
try: extract_data(d) except: skip_this_one(d)
It’s considered poor practice to catch all exceptions this way. Instead, python provides a mechanism to specify just certain kinds of exceptions that you’ll catch (for example, just catching exceptions of type KeyError, which happens when a key is missing from a dictionary.
try: extract_data(d) except KeyError: skip_this_one(d)
Standard Exceptions BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exception +-- StopIteration +-- StopAsyncIteration +-- ArithmeticError | +-- FloatingPointError | +-- OverflowError | +-- ZeroDivisionError +-- AssertionError +-- AttributeError +-- BufferError +-- EOFError +-- ImportError +-- 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
try: