Question: Exception Handling (adapted from #9) Save the file as ch6_ex3.py Exceptions offer a gracious way to exit a program when things go wrong. Modify the

Exception Handling (adapted from #9) Save the file as ch6_ex3.py

Exceptions offer a gracious way to exit a program when things go wrong. Modify the program you wrote for exercise 2 to handle the exceptions (See 6.4 Exceptions on page 341). Test the program using the following text files (download from BlackBoard assignment page):

6-3-lines1.txt

6-3-lines2.txt

Your program must:

Include a try block in the main function that holds most of the code from exercise 2.

The try block must handle any:

o IOError exceptions are raised when an attempt is made to open the file.

o ValueError exceptions are raised when the items read from the file cannot be converted to type float.

Include all requirements outlined in your program must from #2.

When you turn in ch6_ex3.py it should be set to read 6-3-lines2.txt. It will not pass CodeGrade if it is set to read 6-3-lines1.txt

For the file 6-3-lines1.txt, the output should look like the following:

Total: 227,595.00

# of Lines: 9.00

Average: 25,288.33

For the file 6-3-lines2.txt, the output should look like the following:

Non-numeric data is found in the file.

If you try to load a file name that does not exist, the output should look like the following:

An error occurred while trying to read the file.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!