![](/img/trans.png)
[英]Java - Difference between throwing an Exception and catching and rethrowing Exception
[英]Is there any performance difference between catching Exception or certain type exception ? - Java
我想知道這兩個塊之間是否存在差異或性能問題:
try{
return Integer.parseInt(numAsString);
}catch (Exception e){
return onErrorInt;
}
和
try{
return Integer.parseInt(numAsString);
}catch (NumberFormatException e){
return onErrorInt;
}
有時甚至在嘗試內部有許多例外,例如:
try{
// open file then try to close
// try to parse integer
// another kind of exception throwing funcitons
}catch (Exception e){
return onErrorInt;
}
和
try{
// open file then try to close
// try to parse integer
// another kind of exception throwing funcitons
}catch (NumberFormatException e){
return // something;
} catch (IOException e){
// return same thing in the exception above
}
。
我正在做的是系統將每天24小時運行,每天重啟1次。
在很多地方,我不關心Exception的類型,我只需要讓我的應用程序一直運行。 所以主要是關於性能的問題。
性能差異? 幾乎沒有。 唯一的成本是迭代ExceptionTable,這是一個非常低調的內存操作。 你可以在這里閱讀關於內部的簡短摘要:
區分異常類型的主要原因是允許開發人員在需要時對不同類型的異常采取不同的操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.