Answers for "try except in python 3"

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
1

try except python

age = input('age:')


try:
    new_age = int(age)
except ValueError: # if age is not convertable to int
    print('please enter a number...')
Posted by: Guest on February-24-2021
3

try except python

try: #try to do the following
  print("Hi there")
except: #If what is meant to happen in (try) fails, do this.
  print("A error happened with the code above")
Posted by: Guest on September-29-2020

Python Answers by Framework

Browse Popular Code Answers by Language