[英]How to go from catch block to try block in java?
I have some logic inside the try block. 我在try块中有一些逻辑。 If exception arises, then I am catching the exception in the catch block.
如果出现异常,那么我将在catch块中捕获异常。
Example 例
try{
// line 1
}catch(SocketException se){
// again goto try block
}
If control comes inside catch block then again I want to execute line 1 in try block but how to go again try block? 如果控件进入catch块内,那么我想再次在try块中执行第1行,但是如何再次尝试try块? Can we use Label?
我们可以使用标签吗?
If you want to loop back to an earlier point in your code, put your code in a loop. 如果您想循环回到代码的较早位置,请将您的代码放入循环中。
while (true) {
try {
// line 1 (something that might throw an exception)
break;
} catch (SocketException se) {
// handle the error
}
}
If the code in your try
block executes successfully, the break
will be encountered, and your loop will exit. 如果
try
块中的代码成功执行,将遇到break
,并且循环将退出。 If a SocketException
is thrown, execution will return the the top of the while
loop and your line 1
will be repeated. 如果抛出
SocketException
,则执行将返回while
循环的顶部,并且将重复line 1
。
If you only want to retry a fixed number of times (to avoid being stuck indefinitely), then you could use a for
loop instead of a while
loop. 如果只想重试固定的次数(以避免无限期地卡死),则可以使用
for
循环而不是while
循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.