简体   繁体   English

Guava前提条件RuntimeExceptions处理

[英]Guava Preconditions RuntimeExceptions handling

As I understood, we use Guava Preconditions to fail fast, before changing some objects states (a nice answer here from stackoverflow). 据我了解,在更改某些对象状态之前,我们使用了Guava前提条件来快速失败( 是stackoverflow的一个不错的答案)。 And this is good. 这很好。 However it throws Runtime exceptions and this is not the favorite exceptions for the user of an application (500 errors and so on ...). 但是,它将引发运行时异常,而这不是应用程序用户所喜欢的异常(500个错误,等等)。 So I need you to give me some help in design. 因此,我需要您在设计方面给我一些帮助。

I have an interface that declares many methods. 我有一个声明许多方法的接口。 Each method has arguments which must be controlled (ex: not null). 每个方法都有必须控制的参数(例如:不为null)。 So in the implementation class I use instructions like the following : 因此,在实现类中,我使用如下指令:

Preconditions.checkNotNull(fooObj);

However the program calling this API might crash due to a runtime exception, that is in this case NullPointerException. 但是,调用此API的程序可能会由于运行时异常(在这种情况下为NullPointerException)而崩溃。

So how do you handle these unchecked exceptions? 那么,如何处理这些未经检查的异常?

Thank you. 谢谢。

-------- EDIT The app layers : --------编辑应用程序层:

  • Data Access Layer 资料存取层

  • API declaring the methods that exchange DTO API声明交换DTO的方法

  • Process implementing the API and checking arguments using Guava 使用Guava处理API并检查参数的过程

  • Webservice depending on the process layer Webservice取决于流程层

A failure of a precondition means that your program has a bug. 前提条件失败意味着您的程序存在错误。 Users should not encounter these unless they've found a bug in your program. 除非他们在您的程序中发现错误,否则用户不应遇到这些错误。

Your program should in general show some kind of error message to users in case of an error, but more to the point, you should get informed so you can fix the bug in the first place. 通常,您的程序应该出现错误的情况下向用户显示某种错误消息,但更重要的是,您应该得到通知,以便可以首先修复该错误。

You handle them by designing your program to ensure they never happen. 您可以通过设计程序来确保它们永远不会发生来处理它们。 These precondition methods are intended to detect bugs and help find exactly where the root cause is, not to verify user input. 这些前提条件方法旨在检测错误并帮助准确找到根本原因,而不是验证用户输入。

If you are defining only the API, not the programs that call it, then you "handle" it by telling people in your documentation that the arguments in question must not be null, and leave the problem of satisfying that requirement to them. 如果仅定义API,而不是定义调用该API的程序,则可以通过告诉文档中的人员所讨论的参数一定不能为null来“处理”该API,并由他们来满足该要求。

If you are writing a calling program as well, first try to make sure the exception just never happens. 如果您也在编写调用程序,请首先尝试确保该异常永远不会发生。 You can also put the call in a try/catch block to catch the NullPointerException , but the purpose of the catch block should be to give you better notification of the bug (eg record a log message or trigger an alert) and the triggering circumstances, and possibly to shut down more gracefully or give a more user friendly error message. 您还可以将调用放在try/catch块中以捕获NullPointerException ,但是catch块的目的应该是使您更好地通知该错误(例如,记录日志消息或触发警报)以及触发情况,并可能更正常地关闭或给出更用户友好的错误消息。 Attempting to recover from the failure should be done with great care or not at all - if this kind of failure happens then something has gone wrong that you did not foresee, and the proper way to recover may not be predictable. 从故障中恢复的尝试应该格外小心或根本不要进行-如果发生这种故障,则说明您没有预料到发生了问题,并且正确的恢复方法可能无法预测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM