Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python 3.5 You decide to upload your recent project to a programming community w

ID: 3575444 • Letter: P

Question

Python 3.5 You decide to upload your recent project to a programming community website for feedback, and one thing that community users keep pointing to is the lack of exception handling. Realizing that they are correct, you decide to rectify the situation and improve your programs… or at least one of them. Take one of the programs you designed over the past couple of weeks and enhance the program with exception handling code. Your program should incorporate the TRY and EXCEPT blocks and handle any errors and exceptions that you can find. For this project: You will submit your python code in either the original .py file, or copied into a .txt file. A screenshot of your code having been executed and demonstrating your exception handling. How to Take a Screenshot. Tips: Exception handling programming takes a bit of forethought. Think about ways that you can make your program malfunction while using it (not by changing the code).

Explanation / Answer

Following program read a file line by line:

import sys


fr = open(r"C:Usersnsharm5Desktopcheggex.txt")
lines = fr.readlines()
for line in lines:
    print(line)

In the above program, we did not handle the case if file is not present. If file is not present in the location, then it will throw exception.

import sys

try:
    fr = open(r"C:Usersnsharm5Desktopcheggex.txt")
    lines = fr.readlines()
    for line in lines:
        print(line)
except IOError:
    print "Could not read file:"

The above code handle the exception(file not found).