在捕获多个Java异常时,如何在不编写重复代码的情况下添加状态信息?(How do I add state information without writing repetitive code wh

编程入门 行业动态 更新时间:2024-10-27 13:30:09
在捕获多个Java异常时,如何在不编写重复代码的情况下添加状态信息?(How do I add state information without writing repetitive code when catching multiple Java exceptions?)

我已经阅读了其他一些关于避免Java catch块重复的帖子。 显然,我真正想要的是“多捕获”,但看到Java 7还没有到来,是否有一个很好的模式让我添加状态到我的异常,然后重新抛出它们,而不是抓住厨房水槽 ?

具体来说,我有一些代码可以进行库调用,它可以抛出异常,但是没有为成功的调试提供足够的上下文。 我发现自己遇到了问题,然后进入,将库调用包装在try / catch中,捕获特定的异常,然后在catch块中添加额外的状态信息并重新抛出捕获的异常。 然后,我一遍又一遍地重复遍历该循环,每次都找到一个新的错误条件来记录。 我结束了

try { make_library_call(); } catch (SomeException e){ throw new SomeException ("Needed local state information is " + importantInfo, e); } catch (SomeOtherException e){ throw new SomeOtherException ("Needed local state information is " + importantInfo, e); } catch (YetAnotherException e){ throw new YetAnotherException ("Needed local state information is " + importantInfo, e); }

我认为我真正希望看到的更多的是

try { make_lib_call(); } catch(Exception e){ e.AddSomeInformationSomehow("Needed Info" + importantInfo); throw e; }

假设在那种情况下, e会在重新抛出时保持其实际运行时类型 - 在这种情况下,即使捕获未选中也可能是正常的,因为我将重新抛出它们,并且它们将受益于携带额外的状态信息同样。 我猜一个替代品可能是这样的

try{ make_lib_call(); } finally { if (/*an exception happened*/) logger.debug("Some state info: " + importantInfo); }

但我不知道怎么写条件。 那是疯了/错吗?

I've read a few other posts like this one about avoiding repetition in Java catch blocks. Apparently, what I really want is "multi-catch", but seeing as Java 7 isn't here yet, is there a good pattern to let me add state to my exceptions, then re-throw them, without catching the kitchen sink?

Specifically, I have some code that makes library calls, which can throw exceptions, but don't provide nearly enough context for successful debuging. I find myself having a problem, then going in, wrapping the library call in a try/catch, catching the specific exception, then adding extra state information in the catch block and re-throwing the caught exception. I then re-iterate through that loop over and over, each time finding a new error condition to log. I wind up with

try { make_library_call(); } catch (SomeException e){ throw new SomeException ("Needed local state information is " + importantInfo, e); } catch (SomeOtherException e){ throw new SomeOtherException ("Needed local state information is " + importantInfo, e); } catch (YetAnotherException e){ throw new YetAnotherException ("Needed local state information is " + importantInfo, e); }

etc etc. I think what I'd really like to see is more along the lines of

try { make_lib_call(); } catch(Exception e){ e.AddSomeInformationSomehow("Needed Info" + importantInfo); throw e; }

assuming that in that case e would keep its actual runtime type when re-thrown -- in this case, even catching unchecked would probably be OK since I'd be re-throwing them, and they'd benefit from carrying the extra state info as well. I guess an alternate might be something like

try{ make_lib_call(); } finally { if (/*an exception happened*/) logger.debug("Some state info: " + importantInfo); }

but I don't know how to write the conditional. Is that crazy / wrong?

最满意答案

你可能期待这个,但 ,没有一个好的 (并且我的意思是干净的)方法来做到这一点。 你可能期待它,因为你下意识地知道,如果有一个很好的模式,这可能不值得添加到Java 7。

它实际上是最终的重新抛出功能,在这种情况下你会发现它非常方便。

You're probably expecting this, but no, there isn't a good (and by good I mean clean) way to do this. You're probably expecting it because you subconsciously knew that if there was a good pattern for doing this, it probably wouldn't have been worth adding to Java 7.

It's actually the final rethrow feature that you'd find really handy in this case.

更多推荐

本文发布于:2023-08-01 02:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1354044.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   情况下   异常   状态   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!