编写库(而不是应用程序)时如何处理异常 - Java [英] How to handle exceptions when writing a library (not an application) - Java

查看:100
本文介绍了编写库(而不是应用程序)时如何处理异常 - Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个RESTful Web服务API编写一个Java Wrapper。



我现在正在尝试清理一些异常处理,不知道采取什么方法这是一个Java程序员使用的工具,所以我无法以最终用户应用程序的方式真正处理它。



如果我有一个包含可能抛出异常的代码的方法(连接),如何让这些异常浮动到最终程序员?这甚至是我应该处理的方式,取决于他们抓住例外情况?等等...

解决方案

我建议您从基础API捕获异常(除非他们真的有意义允许)并抛出一个更适合您的抽象级别的新异常。



使用异常链接如果您不想放弃异常的原因。


I'm currently writing an Java Wrapper for a RESTful web services API.

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.

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.

Use exception chaining if you don't feel like discarding the cause of the exception.

这篇关于编写库(而不是应用程序)时如何处理异常 - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆