2014-09-24 28 views
2

在文檔multiple try-catch中有一個聲明。指定值以捕獲參數

If a catch block handles more than one exception type, then the catch parameter is implicitly final. In this example, the catch parameter ex is final and therefore you cannot assign any values to it within the catch block

但我不明白這一點。 cannot assign any values是什麼意思?據我所知,我可以使用exception對象的所有方法。他們在談論什麼assignment

回答

8

如果您使用的是multi-catch,則不能更改Exception參考;

try { 
} catch (IOException|SQLException ex) { 
    ex = null; // <-- NOT LEGAL, the ex is final. 
} 
+1

LoL。它非常明顯。我失敗了,真的很難。 – lapots 2014-09-24 20:10:19

2

這意味着你不能重新分配對象e到別的東西,因爲最終的對象只能分配一次。

try { 
} catch(SomeException | SomeDifferentException e) { 
    e = new FooException(); //Invalid 
    or 
    e = someOtherExcpetionObject; //Invalid 
}