我有標準的try-catch-finally塊:是否有一個很好的方法來定義catch塊是否運行?
try
{
//Action
}
catch(Excteption e)
{
//Catch block
}
finally
{
// Action which would be run in case if catch block was not run
// Action which would be run anyway
}
我想知道是否有某種方式或知道catch塊的內部代碼是不運行,運行特定的代碼在finally塊中,請注意,我明白我可以創建一個布爾值來將它放到catch塊以檢測它的執行情況,但這似乎是一種難以管理的事情:)
請分享您的解決方案, 謝謝:)
編輯: 最後我用Jeroen提出的稍微修改過的版本:
Func<string> Action = null;
try
{
Action =() =>
{
//Action
return true;
};
}
catch(Exception e)
{
Action =() =>
{
//Catch block
return false
}
finally
{
if(Action())
//Action which would be run in case if catch block was not run
// Action which would be run anyway
}
謝謝!
_「我明白我可以在catch塊中創建一個bool值」_否,它必須在 –
之外聲明@TimSchmelter在我讀取評論之前修復了一秒鐘:) – Kiseki
如何移動//動作如果catch塊沒有運行到'try'塊中,在'// Action'之後運行? –