1try:
2 print("I will try to print this line of code")
3except:
4 print("I will print this line of code if an error is encountered")
1try:
2 print("I will try to print this line of code")
3except ERROR_NAME:
4 print("I will print this line of code if error ERROR_NAME is encountered")
1try:
2 Age = int(input("Your Age:- "))
3except ValueError:
4 print("Age not in Intger form")
1age = input('age:')
2
3
4try:
5 new_age = int(age)
6except ValueError: # if age is not convertable to int
7 print('please enter a number...')
1# Python try: except:
2
3try:
4 print(a + b) # Program will try the add b to a
5except:
6 print("There was an error") # If the program will have an error in the try block
7 # The except block will run
8 # except block will run and then the program will continue to run
9
10# Examples:
11a = 1
12b = 1 # <===== no error, except block skipped
13
14a = 1
15b = 'one' # <===== error, except block run
1try: #try to do the following
2 print("Hi there")
3except: #If what is meant to happen in (try) fails, do this.
4 print("A error happened with the code above")
1def sum_of(x, y):
2 try:
3 print(x + y)
4 except TypeError:
5 print("Invalid argument specified.")