Question: There are two general types of errors in Python: syntax errors and exceptions. Syntax errors are just parsing errors, and mostly occur when programming. Exceptions

There are two general types of errors in Python: syntax errors and exceptions. Syntax errors are just parsing errors, and mostly occur when programming. Exceptions are raised by Python when errors occur during the execution of a program or command. Exceptions can be handled in programs, and you can even create your own classes of exceptions. As specified in the code standard (10.3.2), you should not be enclosing large portions of your code in a try-catch loop, and your error handline should be problem specific. You should also never use try-catch when an if-else structure works better. As an example, the following program uses a try-except to handle a users input: try: my_num = int(input(Enter an integer: ) except ValueError: print(Could not convert that to an integer!) If the int() command cannot convert the argument into an integer, it will raise a ValueError. If we catch this error when it happens, the program will not immediately end. Write a Python function get_contents_of_file(filename), that returns an array of the data in a file, where each item in the array is a line in the file, and filename is the name of the file to reach. Its very possible that a user might call this function on a file that does not exist, is currently locked by another process, or is not accessible based on the users permissions. Your function must be able to catch these exceptions (FileNotFoundError, IOError, or PermissonError) and return an empty array if they are raised.

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!