'How do I continue my Python script after a error

I'm trying to make my own OS using:

command = input('['+ location + ']$ ')
if command == 'exit':
    break
elif command == 'open app':
    try:
        app = input('App Name: ')
    except FileNotFoundError():
        print('no such application')
    exec(open('/home/zozijaro/Desktop/ZoziOS/Shell/DownloadedApps/' + app).read())
else:
    print('unknown command: ' + command)

When I enter an app name that does not exist, I expect it to print No Such Application, and then I should get the chance to try again, but instead I receive this error:

Traceback (most recent call last):
File "/home/zozijaro/Desktop/ZoziOS/Kernel/terminal/terminal.py", line 11, in <module>
exec(open('/home/zozijaro/Desktop/ZoziOS/Shell/DownloadedApps/' + app).read())
FileNotFoundError: [Errno 2] No such file or directory: '/home/zozijaro/Desktop/ZoziOS/Shell/DownloadedApps/app'

How can I fix this?



Solution 1:[1]

You need to have the exec statement inside try block.

Solution 2:[2]

Note that the except FileNotFoundError follows a try block in which you don't try to open any file, thus the try block never raises a FileNotFoundError, and the except block never gets executed.

You should move the exec(open... backwards to the try block.

Solution 3:[3]

Replace

    try:
        app = input('App Name: ')
    except FileNotFoundError():
        print('no such application')
    exec(open('/home/zozijaro/Desktop/ZoziOS/Shell/DownloadedApps/' + app).read())

with

    try:
        exec(open('/home/zozijaro/Desktop/ZoziOS/Shell/DownloadedApps/' + app).read())
        app = input('App Name: ')
    except FileNotFoundError:
        print('no such application')

Note that I removed the parenthesis around FileNotFoundError.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 krysta24
Solution 2 Nir H.
Solution 3 Ann Zen