CS1101 Discussion Assignment Unit 8 - Catching file errors with try: except: blocks

Questions

Describe how catching exceptions can help with file errors. Write three Python examples that actually generate file errors on your computer and catch the errors with try: except: blocks. Include the code and output for each example in your post.

Describe how you might deal with each error if you were writing a large production program. These descriptions should be general ideas in English, not actual Python code.

1. Question 1

1.1. Question

Describe how catching exceptions can help with file errors. Write three Python examples that actually generate file errors on your computer and catch the errors with try: except: blocks. Include the code and output for each example in your post.

1.2. Answers

1.2.1. Description

Catching exceptions means “handling an exception with a try and except statement” (Downey, 2015, p. 145). Under normal circumstances, if an exception occurs, the interpreter will print the exception information and terminate the program. But we can use try and except clauses to catch and handle the exception to prevent it from terminating the program. In other words, the program can continue to run after handling the exception.

When we use try and except clauses to catch file exceptions, “Python starts by executing the try clause. If all goes well, it skips the except clause and proceeds. If an exception occurs, it jumps out of the try clause and runs the except clause” (Downey, 2015, p. 141), and we can handle the exception in the except clause, like print the error message.

It is worth mentioning that when we print exception information, we can use this statement: traceback.print_exc(), which can print out detailed exception information.

1.2.2. Examples

Example 1:
Codes:

1
2
3
4
5
6
import traceback

try:
fin = open('hello.txt')
except:
traceback.print_exc()

Outputs:

1
FileNotFoundError: [Errno 2] No such file or directory: 'hello.txt'

Explanations:
There was no file called ‘hello.txt’ in the current directory, so when I used the open function to read it, the statement traceback.print_exc() helped print out the detailed error message: FileNotFoundError: [Errno 2] No such file or directory: ‘hello.txt’. It showed that this was a FileNotFoundError.

Example 2:
Codes:

1
2
3
4
5
6
import traceback

try:
fin = open('dir')
except:
traceback.print_exc()

Outputs:

1
PermissionError: [Errno 13] Permission denied: 'dir'

Explanations:
There was a directory called ‘dir’ in the current path, but it was a directory, not a file. Because “the built-in function open needs to take the name of the file as a parameter” (Downey, 2015, p. 83), so when I used the open function to read it, the statement traceback.print_exc() helped print out the detailed error message: PermissionError: [Errno 13] Permission denied: ‘dir’. It showed that this was a PermissionError.

Example 3:
Codes:

1
2
3
4
5
6
7
import traceback
import os

try:
dirs = os.listdir('output.txt')
except:
traceback.print_exc()

Outputs:

1
NotADirectoryError: [WinError 267] The directory name is invalid: 'output.txt'

Explanations:
I found that it was not easy to generate a NotADirectoryError. In this case, we can use the os module to help create such kind error. Specifically, the os module has a method named listdir, and it takes a path (a directory) as a parameter and returns all the directories and files in this path. We can call this method and pass a file instead of a directory as a parameter. In this way, we can get a NotADirectoryError. As you can see, the statement traceback.print_exc() helped print out the detailed error message: NotADirectoryError: [WinError 267] The directory name is invalid: ‘output.txt’. It showed that this was a NotADirectoryError.

1.3. Pendix

Codes in the IDE:
Codes

Outputs in the IDE:
Outputs

2. Question 2

2.1. Question

Describe how you might deal with each error if you were writing a large production program. These descriptions should be general ideas in English, not actual Python code.

2.2. Answers

There are two main ways to handle exceptions in actual development: one is to handle them directly in the except statement, such as modifying errors or printing prompt messages; the other is to use the raise statement to throw exceptions and let the caller of the code handle them.

When we write a large product project and release it, our program usually runs on the server instead of Windows. Generally, there is no graphical interface on the server. Therefore, the exception information is not directly printed on the console screen. We typically write these exception messages to a log file, and then the developer or maintainer can view the log file and fix the error. Hence, for the FileNotFoundError, PermissionError, and NotADirectoryError, I will use the try and except clauses to catch the exception and write the detailed error messages into a log file. If needed, I will also fix the errors simultaneously, like writing some codes to correct the mistakes. It depends on the specific situations.

Word count: 699

References

1.Downey, A. (2015). Think Python. Green Tea Press.
https://greenteapress.com/thinkpython2/thinkpython2.pdf

​


This is the ending, thanks for reading.