Answers for "try-except block can have _____________ except statements?"

2

try except

try:
    print("I will try to print this line of code")
except:
    print("I will print this line of code if an error is encountered")
else:
    print("I will print this line of code if there's no error encountered")
finally:
    print("I will print this line of code even if there's an error or no error encountered")
Posted by: Guest on September-03-2021
0

Exception Type with except block:

#!/usr/bin/env python3

try:
    with open('input.txt', 'r') as myfile:
        for line in myfile:
            print(line)
except FileNotFoundError:
    print('Sorry, file doesn't exist')
except PermissionError:
    print('Sorry, you don't have permission to access the file')

print('Outside the with block')
Posted by: Guest on November-03-2021

Python Answers by Framework

Browse Popular Code Answers by Language