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?