Advertisement
cfabio

Exception.py

Jan 6th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. #catch exception
  2.     try :
  3.         #throws exception if the input is not a number
  4.         number = int(input())
  5.         print(str(number))
  6.     except ValueError:
  7.         print('error in input')
  8.  
  9. printNumberInput()
  10.  
  11. #raise exception
  12. def myFunction(symbol)
  13.     if len(symbol) != 1 :
  14.         raise Exception('The symbol lenght should be one')
  15.     print(symbol)
  16.  
  17. #avoid to crash when we raise an exception but log all the
  18. #errors as callstack to a separated log file
  19. import traceback
  20. try :
  21.     raise Exception('Error message')
  22. except :
  23.     errorFile = open('error_log.txt','a')
  24.     errorFile.write(traceback.format_exc())
  25.     errorFile.close()
  26.     print('wrote callstack to the error log file')
  27.  
  28.  
  29.  
  30. #assertion
  31. assert False, 'exception message'  # they are threated as exception
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement