Pythonプログラミングの講義26日目。
今日は例外処理の基礎。
【講義内容】
- 例外処理
【ワンポイント】
例外処理
単純な例外処理
try: # todo except: # error todo
例外の有無で処理を分ける
while True: try: a, b = [int(n) for n in input().split()] print(a / b) except Exception as e: print(f'Error: {e}') else: print('no error: todo') if a==0 and b==0: break
例外関係なく必ず処理
while True: try: a, b = [int(n) for n in input().split()] print(a / b) except Exception as e: print(f'Error: {e}') finally: print('anytime: todo') if a==0 and b==0: break
例外の捕捉
特定の例外と、その他の例外
try: # todo except ZeroDivisionError as e: # ZeroDivision Error todo except Exception as e: # Other Error todo
複数の例外を同一処理
try: # todo except (ZeroDivisionError, ValueError) as e: # ZeroDivision Error and Value Error todo
全例外
- Ctrl + c: KeyboardInterruptなど
try: # todo except: # error todo
【今日の積み上げ】
- Python基礎