error handling in python code example
Example 1: exception pyton print
except Exception as e: print(e)
Example 2: python try catch
try:
except ValueError:
except (BadThingError, HorrbileThingError) as e:
except:
else:
finally:
Example 3: try catch python
try:
print("try to run this block")
except:
print("run this bock if there was error in earlier block")
Example 4: error handling Pyhtin
try:
age = int(input("Please enter your age: "))
print(age)
except:
print("please enter a number instead! ")
print('bye')
while True:
try:
age = int(input("Please enter your age: "))
print(age)
except:
print("please enter a number instead! ")
else:
break
Example 5: except as Exception:
>>> def catch():
... try:
... asd()
... except Exception as e:
... print e.message, e.args
...
>>> catch()
global name 'asd' is not defined ("global name 'asd' is not defined",)
Example 6: error handling in python
try:
print(x)
except SyntaxError:
print("There is a SyntaxError in your code")
except NameError:
print("There is a NameError in your code")
except TypeError:
print("There is a TypeError in your code")