简体   繁体   English

如何在编写库(而不是应用程序)时处理异常 - Java

[英]How to handle exceptions when writing a library (not an application) - Java

I'm currently writing an Java Wrapper for a RESTful web services API. 我目前正在为RESTful Web服务API编写Java Wrapper。

I'm now trying to clean up some of the exception handling, and not sure what approach to take. 我现在正在尝试清理一些异常处理,并且不确定采取什么方法。 This is a tool intended to be used by Java programmers, so I can't really handle it the same way I would with a end-user application. 这是一个供Java程序员使用的工具,因此我无法像使用最终用户应用程序那样处理它。

If I have a method (connection) that contains code that could throw exceptions, how do I get those exceptions to float up to the end-programmer? 如果我有一个方法(连接)包含可能抛出异常的代码,我如何让这些异常浮动到最终程序员? and is this even the way I should handle it, depending on them to catch the exceptions? 这是否是我应该处理它的方式,取决于它们来捕捉异常? etc... 等等...

I suggest you catch the exceptions from the underlying API (unless they really make sense to allow through), and throw a new exception which is more appropriate for your level of abstraction. 我建议你从底层API中捕获异常(除非它们真的有意义允许通过),并抛出一个更适合你的抽象级别的新异常。

Use exception chaining if you don't feel like discarding the cause of the exception. 如果您不想丢弃异常原因,请使用异常链接

I think you should decide whether the existing type is specific to the implementation, or inherent to the library. 我认为你应该决定现有的类型是特定于实现还是库固有的。 For example, if it's a network related exception and you're obviously making a network-based API, I'd just let it propagate up. 例如,如果它是一个与网络相关的异常,并且您显然正在制作基于网络的API,那么我只是让它传播开来。 The caller needs to be aware of that sort of error anyway. 无论如何,调用者需要知道这种错误。

On the other hand, if it's a database -related exception which is only possible because for some bizarre reason you're looking up the WSDL in an embedded database, or something like that, that's clearly inappropriate for the caller to have to deal with - so catch it and wrap it in an exception which is more appropriate to your abstract level. 另一方面,如果它是一个与数据库相关的异常,这只是因为某些奇怪的原因你在嵌入式数据库中查找WSDL,或类似的东西,这显然不适合调用者必须处理 -所以捕获它并将其包装在一个更适合您的抽象级别的异常中。

You will have to pass the exception to the user in any case since it's a library. 在任何情况下,您都必须将异常传递给用户,因为它是一个库。

  • If you are not logging and are not planning to create custom exceptions, then you just don't have to handle the exception. 如果您没有记录并且不打算创建自定义异常,那么您就不必处理异常。
  • if you are logging, handle the exception and rethrow the exception. 如果您正在记录,请处理异常并重新抛出异常。
  • if you have custom exceptions, make sure it have take exception a constructor parameter and then link the current exception to your current exception and then throw custom exception. 如果您有自定义异常,请确保它具有构造函数参数异常,然后将当前异常链接到当前异常,然后抛出自定义异常。 This is required to maintain the useful stack trace information. 这是维护有用的堆栈跟踪信息所必需的。

The question is how opaque you want your library to be. 问题是你希望你的图书馆是多么不透明。

Every exception type that you throw to your users should imply the user can do something about it. 您抛给用户的每种异常类型都应该暗示用户可以对此做些什么。 For example, 例如,

catch (ConnectionException e) {
  disconnect();
  connectAgain();
}

only works if your users have access to disconnect() and connectAgain(). 仅当您的用户有权访问disconnect()和connectAgain()时才有效。 If, however, you promise to provide all kinds of connectivity, your code should already have this logic, and if that fails, throw a generic WrapperException and be done with it. 但是,如果您承诺提供各种连接,那么您的代码应该已经具有此逻辑,如果失败,则抛出一个通用的WrapperException并完成它。

Possibly a good approach for you would be declaring your own type of exception (and don't make it a RuntimeException), catching the exceptions you observe and throwing your exception instead. 可能一个好的方法是声明自己的异常类型(并且不要使它成为RuntimeException),捕获您观察到的异常并抛出异常。

I think its important what the API does, and in which context it is used. 我认为它的重要性在于API的作用以及使用它的上下文。

If the API is part of your presentation/rendering layer, then I would prefer to always return something that is ready to be rendered, decorated, or written to the response stream. 如果API是演示文稿/渲染层的一部分,那么我宁愿总是返回一些可以渲染,修饰或写入响应流的内容。

If the API is meant to perform (non-rendering/UI related) processing, I would have no problem throwing up any exceptions outised the scope of the API logic. 如果API要执行(非呈现/ UI相关)处理,那么抛出API逻辑范围之外的任何异常都没有问题。

If the API is designed well, these would be causes that are clearly beyond the control of the API, or logically beyond the scope of what the API "knows how to" or "should" handle, even if it could catch/control it. 如果API设计得很好,那么这些原因显然超出了API的控制范围,或者逻辑上超出了API“知道如何”或“应该”处理的范围,即使它可以捕获/控制它。

When returning an exception to a "user" I prefer returning standard exceptions whenever possible rather than a single custom wrapper type. 将异常返回给“用户”时,我更愿意尽可能返回标准异常,而不是单个自定义包装类型。

Within my API implementation however, I use custom exception types frequently if they serve a clear and useful purpose. 但是,在我的API实现中,如果它们具有明确且有用的用途,我会经常使用自定义异常类型。

just my 2 cents :) 只是我的2美分:)

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

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