在catch块中捕获异常后,是否可以再次在try块中执行代码? [英] Is it possible to execute the code in the try block again after an exception in caught in catch block?
本文介绍了在catch块中捕获异常后,是否可以再次在try块中执行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在捕获异常后再次在try块中执行代码。
I want to execute the code in the try block again after an exception is caught. Is that possible somehow?
例如:
try
{
//execute some code
}
catch(Exception e)
{
}
如果捕获到异常,我想再次进入try块以执行一些代码,然后再次尝试执行它。
If the exception is caught I want to go in the try block again to "execute some code" and try again to execute it.
推荐答案
将其放入循环中。
bool tryAgain = true;
while(tryAgain){
try{
// execute some code;
// Maybe set tryAgain = false;
}catch(Exception e){
// Or maybe set tryAgain = false; here, depending upon the exception, or saved details from within the try.
}
}
请小心避免无限循环。
更好的方法可能是将某些代码放入其自己的方法中,然后您可以根据需要在try和catch中同时调用该方法。
A better approach may be to put your "some code" within its own method, then you could call the method from both within the try and the catch as appropriate.
这篇关于在catch块中捕获异常后,是否可以再次在try块中执行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文