什么Java异常类用于HTTP错误? [英] What Java exception class to use for HTTP errors?

查看:107
本文介绍了什么Java异常类用于HTTP错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apache HttpClient ,并希望传达HTTP错误(400 Bad请求,404未找到,500服务器错误等)通过Java异常机制到调用代码。在Java标准库中还是在广泛使用的库中有一个例外情况,适用于此目的使用或子类化?

I am using Apache HttpClient and would like to communicate HTTP errors (400 Bad Request, 404 Not Found, 500 Server Error, etc.) via the Java exception mechanism to the calling code. Is there an exception in the Java standard library or in a widely used library that would be appropriate to use or to subclass for this purpose?

替代方法是检查状态返回码。这似乎是HttpClient的设计理念,但是由于这些错误在我的应用程序中是非常特别的,所以我希望在发生时为我设置堆栈跟踪和其他很好的异常事件。

The alternative is to check status return codes. This appears to be the HttpClient design philosophy, but since these errors are truly exceptional in my app, I would like to have the stack trace and other nice exception things set up for me when they happen.

推荐答案

如果它不是HttpClient设计哲学中的异常,而是在代码中的异常,那么创建自己的异常类。 (作为org.apache.commons.httpclient.HttpException的子类)

If it's not an Exception in HttpClient design philosophy, but an Exception in your code, then create your own Exception classes. ( As a subclass of org.apache.commons.httpclient.HttpException )

这篇关于什么Java异常类用于HTTP错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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