Advertisement
Mr_D3a1h

Exception Intercepter

Apr 16th, 2023
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.47 KB | None | 0 0
  1. class ExceptionInterceptor:
  2.     def __init__(self, *exceptions):
  3.         self.exceptions = exceptions
  4.  
  5.     def __enter__(self):
  6.         pass
  7.  
  8.     def __exit__(self, exc_type, exc_value, traceback):
  9.         if exc_type in self.exceptions:
  10.             return True
  11.         else:
  12.             return False
  13. with ExceptionInterceptor(ValueError):
  14.     value = input("Enter a number: ")
  15.     try:
  16.         value = int(value)
  17.     except ValueError:
  18.         print("Invalid input!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement