2017-10-10 56 views
1

我是新來的蟒蛇,我一直想知道如果你可以在Python中做這樣的事情。在Python中的例外(多個嘗試塊)

try: 
    something(): 
if it throws an exception then try this: 
    something1(): 
if it throws an exception again: 
    print(exception) 
+1

你可以在另一個'try'塊或者'except'塊中放一個'try'塊。 – khelwood

回答

1

當然可以。

try: 
    something() 
except Exception as exp: 
    try: 
     something1() 
    except Exception as exp2: 
     print(exp, exp2) 
     raise exp # in case you want to raise the original exception