如何创建一个 try-catch 块,不断调用对象上的方法,直到不再有异常需要捕获
基本上迭代一个列表,
- 调用第一个对象的方法
- 捕获第一个异常(如果有);如果没有更多异常需要捕获,则正常返回。否则,继续调用方法,直到捕获所有异常。
- 移动到下一个对象。
我可以迭代每个对象,调用该方法并捕获一个异常,但我不知道如何连续调用它的方法并继续捕获异常。
Basically iterating through a list and,
- Invoke method on first object
- Catch the first exception (if any); if there are no more exceptions to catch, return normally. Otherwise, keep on invoking method until all exceptions are caught.
- Move on to next object.
I can iterate through each object, invoke the method, and catch one exception but I do not know how to continuously invoke the method on it and keep on catching exceptions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这与其他答案类似,但没有标志,这对我来说似乎很混乱。不过我不太明白这个问题,所以我只是把它扔在那里以防它有用。
这种方法取决于未标记的
的操作break
语句,它突然完成,然后正常退出封闭的while
语句。根据随后的评论,我认为当一个方法声明要抛出多个异常时,它的含义存在一些混乱。
方法的每次调用都可以通过抛出一个异常来终止。您无法以某种方式从中断处恢复调用并处理后续异常。
因此,如果一个方法抛出多个异常,请捕获一个共同的祖先,然后继续。例如,如果一个方法抛出
java.io.EOFException
或java.nio.channels.ClosedChannelException
,您可以简单地捕获java .io.IOException
因为它是一个共同的祖先。 (出于同样的原因,您也可以捕获java.lang.Exception
或java.lang.Throwable
。)在相同条件下再次调用该方法不会得到任何结果。更远。如果您想尝试在每个对象上调用该方法,即使某些对象失败,请使用以下命令:
This is similar to the other answers, but without the flag, which seems like clutter to me. I don't really understand the question though, so I'm just throwing it out there in case it is useful.
This approach hinges on the operation of the unlabeled
break
statement, which completes abruptly and then exits the enclosingwhile
statement normally.Based on subsequent comments, I think there is some confusion about what it means when there are multiple exceptions declared to be thrown by a method.
Each invocation of a method can be terminated by just one exception being thrown. You can't somehow resume invocation where it left off, and handle subsequent exceptions.
So, if a method throws multiple exceptions, catch a common ancestor, and move on. For example, if a method throws
java.io.EOFException
orjava.nio.channels.ClosedChannelException
, you could simply catchjava.io.IOException
since it is a common ancestor. (You could also catchjava.lang.Exception
orjava.lang.Throwable
for the same reason.) Invoking the method again under the same conditions won't get you any further.If you want to attempt to invoke the method on each object, even if some fail, use this:
如果您正在谈论处理将抛出多个异常的单个方法调用,则无法完成 - 无论您调用该方法多少次,它都会继续抛出第一个异常。你不能返回该方法并从那里继续运行;抛出一个异常后,一切就结束了。
但是,如果您正在讨论的方法有时会引发异常,有时不会,请尝试如下操作:
If you're talking about dealing with a single method call that will throw more than one exception, it can't be done -- no matter how many times you call the method, it will keep on throwing the first exception. You can't go back into the method and keep running from there; after throwing one exception, it's all over.
But if you're talking about a method that sometimes throws exceptions and sometimes doesn't, try something like this:
这是我的理解。
您有一个对象的方法,它可能会引发许多异常。
您要做的就是捕获所有对象并继续处理列表中的下一个对象。
这是正确的吗?
因此,那就是:
当您执行此操作时,如果调用
thatMethod()
抛出异常,并且该异常在catch
部分中列出,则执行流程将跳转到该异常,之后它将继续正常流程(即 for 循环并将继续处理下一个对象)我希望这就是所需要的。有关详细信息,请阅读:catch 块 Java 教程部分 基本类
This is what I understand.
You have an object's method which may throw a number of exceptions.
What you want to do is to catch them all and continue with the next object in the list.
Is that correct?
So, that would be:
When you do this, if the invocation of
thatMethod()
throws an exception and that exception is listed in thecatch
section, the execution flow will jump to that exception and after it will continue to the normal flow ( which is thefor
loop and will continue with the next object )I hope this is what to need. For more information read: The catch block in the Java Tutorial section Essential classes
我假设您正在尝试对列表中的项目执行某种验证,其中通过抛出异常来报告验证错误。我还假设您正在尝试收集所有验证错误。
简单的答案是,这个问题无法用这种方法解决。要理解原因,请看一下:
这会失败,因为每次调用
item.doSomething()
时,它都会在完全相同的位置引发异常。最终结果是无限循环。使用此方法最多可以捕获列表中每个
项目
的第一个异常。那么如何才能实现您想要实现的目标呢?答案是您必须更改验证代码以使用抛出异常之外的其他方式来报告错误。例如,您可以将:更改
为类似这样的内容:
Messy huh!您可以通过各种方式对此进行补充,但这种错误报告方式总是会更加复杂。但我认为没有一种实用的方法可以避免混乱并捕获所有验证错误。
I'm assuming that you are trying to performs some kind of validation to the items in a list, where the validation errors are reported by throwing exceptions. I'm also assuming that you are trying to collect all of the validation errors.
The simple answer is that this problem cannot be solved using this approach. To understand why, take a look at this:
This fails because each time you call
item.doSomething()
it is going to throw an exception at exactly the same place. The net result is an infinite loop.The best you can do with this approach is to capture the first exception for each
item
in the list.So how can you achieve what you are trying to achieve? The answer is that you have to change the validation code to use some other way to report errors than throwing exceptions. For example, you could change:
to something like this:
Messy huh! You could sugar this in various ways, but this style of error reporting is always going to be more complicated. But I don't think there is a practical way to avoid the messiness AND capture ALL of the validation errors.