java Jackson 和那个可怕的 IOException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7471216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:07:53  来源:igfitidea点击:

Hymanson and that dreaded IOException

javajsonexceptionHymansonconvention

提问by wulfgarpro

Hymanson'sObjectMapper#readValuemember throws three checked exceptions:

Hymanson 的ObjectMapper#readValue成员抛出三个受检异常:

IOException 
JsonParseException 
JsonMappingException
IOException 
JsonParseException 
JsonMappingException

JsonParseExceptionand JsonMappingExceptionextend IOException. I want to wrap the two aforementioned child classes and throw my own custom exceptions, yet, the base class, IOException, being checked, requires me to either catch or throw it also.

JsonParseExceptionJsonMappingException扩展IOException。我想包装前面提到的两个子类并抛出我自己的自定义异常,但是IOException,被检查的基类要求我也捕获或抛出它。

It doesn't make sense for me to throw the IOExceptionup to the calling layer, but, adversely, it's a smell if I hide it. My original thought was to not catch it and leave it to the caller/run-time exception mechanism to deal with it... yet, I don't want to have to force the caller to catch or specify.

把它扔到IOException调用层对我来说没有意义,但相反,如果我把它隐藏起来,它就会有味道。我最初的想法是不捕捉它并将它留给调用者/运行时异常机制来处理它......然而,我不想强​​迫调用者捕捉或指定。

What does one do in such a situation?

在这种情况下怎么办?

采纳答案by wulfgarpro

Short answer: if you deal with IO, you deal with IOExceptions. If you don't deal with IO, then IOExceptions should be turned into unchecked exceptions, because they're symptoms of buggy code.

简短的回答:如果你处理 IO,你就处理IOExceptions。如果你不处理 IO,那么IOExceptions 应该变成未经检查的异常,因为它们是错误代码的症状。



Longer answer:

更长的答案:

readValuealways takes a JsonParser, which may be wrapped around IO (e.g. a file or a URL). If you're dealing with IO, there's no way around dealing with IOExceptions, and you should either handle them or rethrow/pass them in some way. Anything can happen during IO, and you should be prepared to deal with the exceptions.

readValue总是需要一个JsonParser,它可以包裹在 IO 周围(例如一个文件或一个 URL)。如果您正在处理 IO,则无法处理IOExceptions,您应该处理它们或以某种方式重新抛出/传递它们。IO 期间任何事情都可能发生,您应该准备好处理异常。

However, if you are sure that your JsonParserinstances don't use IO (e.g. you used JsonFactory#createJsonParser(java.lang.String)to create a JSON parser on a string), you may assume that any IOExceptions you receive are bugs, either in your code or in Hymanson. Usually, throwing an unchecked exception is then the proper way to deal with it:

但是,如果您确定您的JsonParser实例不使用 IO(例如,您曾经JsonFactory#createJsonParser(java.lang.String)在字符串上创建 JSON 解析器),您可以假设IOException您收到的任何s 都是错误,无论是在您的代码中还是在 Hymanson 中。通常,抛出未经检查的异常是处理它的正确方法:

ObjectMapper om = new ObjectMapper(/* whatever */);
JsonParser jp = JsonFactory.createJsonParser("{ \"foo\": \"bar\" }");
try {
    return om.readValue(jp);
} catch (IOException e) {
    throw new AssertionError("An IOException occurred when this was assumed to be impossible.");
}

Nota bene: my Java is rusty and I've never used Hymanson, so consider the above block to be pseudocode.

注意:我的 Java 已经生锈了,而且我从未使用过 Hymanson,因此将上述块视为伪​​代码。

In any case, you never need to declare AssertionErrorin throws, because they're unchecked exceptions. Everything that's a subclass of java.lang.RuntimeExceptionor java.lang.Errordoesn't need to be caught or rethrown explicitly. These exceptions are used for problems that are not expected to occur unless you're dealing with buggy code or when your VM's host is on fire.

在任何情况下,您都不需要声明AssertionErrorin throws,因为它们是未经检查的异常。所有属于java.lang.RuntimeExceptionjava.lang.Error不需要明确捕获或重新抛出的子类。这些异常用于处理预计不会发生的问题,除非您正在处理有缺陷的代码或当您的 VM 主机着火时。

回答by StaxMan

While this is not clearly documented within Hymanson, rationale for IOExceptions is simple: IOExceptions from input sources (and output targets) are thrown as is -- since Hymanson itself can not do anything for these, they are thrown as is. The only additional source for IOExceptions are things that are conceptually part of low-level (data-format independent) I/O handling, specifically, decoding of characters encodings like UTF-8.

虽然这在 Hymanson 中没有明确记录,但 IOExceptions 的基本原理很简单:来自输入源(和输出目标)的 IOExceptions 按原样抛出 - 由于 Hymanson 本身无法为这些做任何事情,它们按原样抛出。IOExceptions 的唯一额外来源是概念上属于低级(独立于数据格式)I/O 处理的东西,特别是像 UTF-8 这样的字符编码的解码。

From this, it seems relatively intuitive that JsonParsingException is for problems related to trying to parse invalid content; and JsonMappingException for problems at data-binding level. a

由此看来,JsonParsingException 是针对尝试解析无效内容的相关问题,似乎比较直观;和 JsonMappingException 用于数据绑定级别的问题。一个

回答by nfechner

You should handle the IOExceptionthe same way you handle the json exceptions and wrap it. As the documentation of Hymanson is lacking so much, you don't really know why any of them are thrown anyway (except for "an unknown error").

您应该IOException像处理 json 异常一样处理并包装它。由于Hyman逊的文档太缺乏了,你真的不知道为什么它们中的任何一个都会被抛出(“一个未知错误”除外)。