Python Tutorial for Beginners - 12. Using Try/Except Blocks for Error Handling

  1. Test file
  2. Currupt file
  3. Run Code 1 in Python
  4. Run Code 2 in Python
  5. Run Code 3 in Python
  6. Run Code 4 in Python
  7. Run Code 5 in Python
  8. Run Code 6 in Python
  9. Run Code 7 in Python
  10. Run Code 8 in Python
  11. Run Code 9 in Python
  12. Run Code 10 in Python
  13. Run Code 11 in Python

Test file

# file: 'test_file.txt'
Test File Contents!

test_file.txt

Currupt file

# file: 'currupt_file.txt'
Currupt File!

currupt_file.txt

Run Code 1 in Python

# file: 'app1.py'
try:
     pass
except Exception:
    pass
else:
   pass
finally:
   pass

Run Code 2 in Python

# file: 'app2.py'
f = open('testfile.txt')

try:
     pass
except Exception:
    pass

Run Code 3 in Python

# file: 'app3.py'
try:
    f = open('testfile.txt')
except Exception:
    print('Sorry. This file doenst exist')

Run Code 4 in Python

# file: 'app4.py'
try:
    f = open('test_file.txt')
    var = bad_var
except FileNotFoundError:
    print('Sorry. This file doenst exist')

Run Code 5 in Python

# file: 'app5.py'
try:
    f = open('test_file.txt')
    var = bad_var
except FileNotFoundError:
    print('Sorry. This file doenst exist')
except Exception:
    print('Sorry. Samting went wrong')

Run Code 6 in Python

# file: 'app6.py'
try:
    f = open('testfile.txt')
except FileNotFoundError as e:
    print(e)
except Exception as e:
    print(e)

Run Code 7 in Python

# file: 'app7.py'
try:
    f = open('test_file.txt')
except FileNotFoundError as e:
    print(e)
except Exception as e:
    print(e)
else:
   print(f.read())
   f.close()

Run Code 8 in Python

# file: 'app8.py'
try:
    f = open('test_file.txt')
except FileNotFoundError as e:
    print(e)
except Exception as e:
    print(e)
else:
   print(f.read())
   f.close()
finally:
   print('Excecuting Finally...')

Run Code 9 in Python

# file: 'app9.py'
try:
    f = open('testfile.txt')
except FileNotFoundError as e:
    print(e)
except Exception as e:
    print(e)
else:
   print(f.read())
   f.close()
finally:
   print('Excecuting Finally...')

Run Code 10 in Python

# file: 'app10.py'
try:
    f = open('curruptfile.txt')
except IOError as e:
    print('First!')
except Exception as e:
    print('Second')
else:
    print(f.read())
    f.close()
finally:
    print("Executing Finally...")

print('End of program')

Run Code 11 in Python

# file: 'app11.py'
try:
    f = open('curruptfile.txt')
    # if f.name == 'currupt_file.txt':
    #     raise Exception
except IOError as e:
    print('First!')
except Exception as e:
    print('Second')
else:
    print(f.read())
    f.close()
finally:
    print("Executing Finally...")

print('End of program')

Comments 💬