0

I am creating a program that lets you launch applications from Python. I had designed it so if a certain web browser was not downloaded, it would default to another one. Unfortunately, the try block seems to only function with one 'except FileNotFoundError.' Is there any way to have multiple of these in the same try block? Here's my (failing) code below:

app = input("\nWelcome to AppLauncher. You can launch your web browser by typing '1', your File Explorer by typing '2', or quit the program by typing '3': ")
    if app == "1":
        try:
            os.startfile('chrome.exe')
        except FileNotFoundError:
            os.startfile('firefox.exe')
        except FileNotFoundError:
            os.startfile('msedge.exe')

If the user does not have Google Chrome downloaded, the program attempts to launch Mozilla Firefox. If that application is not found, it should open Microsoft Edge; instead it raises this error in IDLE (Please note that I have purposely misspelled chrome.exe and firefox.exe in order to simulate the programs essentially not existing):

Traceback (most recent call last):
  File "C:/Users/NoName/AppData/Local/Programs/Python/Python38-32/applaunchermodule.py", line 7, in <module>
    os.startfile('chome.exe')
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'chome.exe'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/NoName/AppData/Local/Programs/Python/Python38-32/applaunchermodule.py", line 9, in <module>
    os.startfile('frefox.exe')
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'frefox.exe'

Is there any way to raise two of the same exceptions in a single try block?

1
  • P.S. Please note that I am new to Python and only taking 101 classes, so try not to use jargon that only experts would understand. Commented Aug 6, 2020 at 18:17

2 Answers 2

1
for exe in ['chrome.exe','firefox.exe','msedge.exe']:
    try:
        os.startfile(exe)
        break

    except FileNotFoundError:
        print(exe,"error")
Sign up to request clarification or add additional context in comments.

Comments

0

For your exact case, I would suggest this:

priority_apps = ['chrome.exe', 'firefox.exe', 'msedge.exe']  # attempts to open in priority order
current_priority = 0
opened_app = False
while not opened_app and current_priority < len(priority_apps):
    try: 
        os.startfile(priority_apps[current_priority])
        opened_app = True
    except Exception as e:
        current_priority += 1
if not opened_app:
    print("couldn't open anything! :(")

generic alternative with functions:

try:
    do_something()
except Exception as e:
    do_something_else1()

def do_something_else1():
    try:
        do_something()
    except Exception as e:
        do_something_else2()

generic alternative with nested try/except:

try: 
    do_something()
except Exception as e: 
    try: 
        do_something_else()
    except Exception as e:
        do_something_better()

2 Comments

Thanks, let me try it out
It worked! I tried nesting my try/excepts, and it functioned how I wanted it to. Thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.