My QA Projects

QA Projects I was involded.

View on GitHub

Exceptions

Even if a statement of expression is syntactically correct, errors may still occure when a program is running.

Image of exception handling structure

Exceptions:

ValueError:

print(int('one handred'))
# int will throw a Value exception
# invalid literal for int()

TypeError:

ZeroDivisionError:

NameError:

IndexError:

mylist = [1,2,3]
print(mylist[10])
#this will cause an indexerror
print('finished')

KeyError:

Catching Exceptions

try:
    try_block
Except exception_type:
    exception_block

# multiple
try:
    try_block
Except exception_type1:
    exception_block1
Except exception_type2:
    exception_block2

Example

mylist = [1,2,3]
try:
    print(mylist[10])
    print('got item at index 10')
except IndexError:
    print('mylist index is out of bounds')
print('finished')

Open function Exceptions

FileExistsError:

FileNotFoundError:

PermissionError:

IsADirectoryError:

try:
    with open('does_not_exist.txt') as f:
        print(f.read())
except PermissionError:
    print('file permission error')
except FileNotFoundError:
    print('does_not_exist.txt' not found)

Exceptions handling questions could be


  Strings   Variables   Lists   Tuples   Dictionary  
  Control   Function   Files   Exceptions      
  OOP   Algorithm   Data Structure   back