OOP 2 Python Exception Handling
OOP 2 Python Exception Handling
What is Exception?
try:
You do your operations here
......................
except ExceptionI:
If there is ExceptionI, then execute this block.
except ExceptionII:
If there is ExceptionII, then execute this block.
......................
else:
If there is no exception, then execute this block.
e.g.
try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
except IOError:
print("Error: can\'t find file or read data")
else:
print("Written content in the file successfully")
fh.close()
Output
Written content in the file successfully
try:
fh = open("testfile", "r")
fh.write("This is my test file for exception handling!!")
except IOError:
print("Error: file not written, check permissions")
else:
print("Written content in the file successfully")
fh.close()
Output
Error: file not written, check permissions