Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在一个';试试';块_Python 3.x_Try Catch - Fatal编程技术网

Python 3.x 如何在一个';试试';块

Python 3.x 如何在一个';试试';块,python-3.x,try-catch,Python 3.x,Try Catch,我正在创建一个程序,允许您从Python启动应用程序。我设计它的目的是,如果某个web浏览器没有下载,它将默认为另一个。不幸的是,try块似乎只有一个'except FileNotFoundError'起作用。有没有办法在同一个try块中有多个'except FileNotFoundError'?下面是我的(失败)代码: app = input("\nWelcome to AppLauncher. You can launch your web browser by typing '1

我正在创建一个程序,允许您从Python启动应用程序。我设计它的目的是,如果某个web浏览器没有下载,它将默认为另一个。不幸的是,try块似乎只有一个'except FileNotFoundError'起作用。有没有办法在同一个try块中有多个'except FileNotFoundError'?下面是我的(失败)代码:

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')
如果用户没有下载Google Chrome,程序将尝试启动Mozilla Firefox。如果找不到该应用程序,它应打开Microsoft Edge;相反,它会在空闲时引发此错误(请注意,我故意将chrome.exe和firefox.exe拼错,以模拟基本上不存在的程序):

回溯(最近一次呼叫最后一次):
文件“C:/Users/NoName/AppData/Local/Programs/Python/Python38-32/applaunchermodule.py”,第7行,在
os.startfile('chome.exe')
FileNotFoundError:[WinError 2]系统找不到指定的文件:“chome.exe”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“C:/Users/NoName/AppData/Local/Programs/Python/Python38-32/applaunchermodule.py”,第9行,在
os.startfile('frefox.exe')
FileNotFoundError:[WinError 2]系统找不到指定的文件:“frefox.exe”

有没有办法在一个try块中引发两个相同的异常?

对于您的确切情况,我建议:

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! :(")
具有嵌套try/except的常规备选方案:

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

另外,请注意,我是Python新手,只上了101门课,所以尽量不要使用只有专家才能理解的术语。谢谢,让我来试试吧,它很管用!我尝试嵌套我的try/excepts,它按照我希望的方式运行。非常感谢。
try:
    do_something()
except Exception as e:
    do_something_else1()

def do_something_else1():
    try:
        do_something()
    except Exception as e:
        do_something_else2()
try: 
    do_something()
except Exception as e: 
    try: 
        do_something_else()
    except Exception as e:
        do_something_better()
for exe in ['chrome.exe','firefox.exe','msedge.exe']:
    try:
        os.startfile(exe)
        break

    except FileNotFoundError:
        print(exe,"error")