Answers for "how to try except"

5

python try except

import sys

try:
    f = open('myfile.txt')
    s = f.readline()
    i = int(s.strip())
except OSError as err:
    print("OS error: {0}".format(err))
except ValueError:
    print("Could not convert data to an integer.")
except:
    print("Unexpected error:", sys.exc_info()[0])
    raise
Posted by: Guest on January-18-2021
2

python try except

try:
  val = 1/0 
except Exception as e:
  raise Exception('ZeroDivisionError')
Posted by: Guest on January-26-2021

Python Answers by Framework

Browse Popular Code Answers by Language