简体   繁体   English

抛出由参数传递给 function - java 的异常

[英]Throwing exception passed by parameter to a function - java

im currently working in a complete generic scenario in which i map a json as string to a dto class.我目前在一个完整的通用场景中工作,其中我 map 将 json 作为字符串连接到 dto class。 That works fine with my function mapJsonToDto but im trying to make it more generic so that the developer who uses this function can also specify what exception to be thrown.这适用于我的 function mapJsonToDto但我试图使其更通用,以便使用此 function 的开发人员也可以指定要抛出的异常。 So they can catch as they like.所以他们可以随心所欲地捕捉。 With this i avoid catching an IOException.这样我就可以避免捕获 IOException。 Letting the function handle everything.让 function 处理一切。

public class MapperUtils {
    public <T extends Throwable> Object mapJsonToDto(Class<?> dtoClass, String jsonDto, T exceptionToThrow) throws IOException {
        Object dto = null;
        try {
            dto = new ObjectMapper().readValue(jsonDto, dtoClass);
        } catch (IOException e) {
            throw new exceptionToThrow();
        }
        return dto;
    }
}

I cannot understand how to pass an exception class instance to a function and throwing that specific as well.我无法理解如何将异常 class 实例传递给 function 并抛出该异常。

Instead of passing the exception to throw (which would then have a completely wrong stack trace), I think you'd want a function that converts an exception from one type to another:我认为您不需要将异常从一种类型转换为另一种类型的 function ,而不是将异常传递给 throw (然后会有完全错误的堆栈跟踪):

public <T extends Throwable, D> D mapJsonToDto(Class<D> dtoClass, String json, Function<IOException, T> exceptionMapper) throws T {
    try {
        return new ObjectMapper().readValue(json, dtoClass);
        // if readValue doesn't do the casting right, try:
        return dtoClass.cast(new ObjectMapper().readValue(json, dtoClass);
    } catch (IOException e) {
        throw exceptionMapper.apply(e);
    }
}

And an example:还有一个例子:

Person p = mapJsonToDto(Person.class, "{name: \"Joe\"}",
  e -> new IllegalArgumentException("malformed JSON", e));

As a general rule, though, this seems like boneheaded design.不过,作为一般规则,这似乎是愚蠢的设计。 If you find the IOException overly general, then you can't handwave the problem away by allowing the caller to provide a no doubt similarly overly general mapper.如果您发现 IOException 过于笼统,那么您不能通过允许调用者提供无疑同样过于笼统的映射器来解决问题。 The only way out for a caller is to do a deep dive on the exception and write, I dunno, an if/elseif block with a ton of levels to it to try to ascertain the real problem eg via analysing the message, which is all sorts of ugly.调用者的唯一出路是深入研究异常并编写(我不知道)一个具有大量级别的 if/elseif 块,以尝试确定真正的问题,例如通过分析消息,仅此而已有点丑。

Either you don't care about that level of detail and you should therefore just stick with IOException (what point is there adding code and pointless layers of indirection?), or you do care and this isn't good enough;要么您不关心该级别的细节,因此您应该坚持使用 IOException(添加代码和无意义的间接层有什么意义?),或者您确实关心这还不够好; you'd want to design a better error system.你想设计一个更好的错误系统。 Except, that's not your job, that'd be ObjectMapper.readValue 's job.除了,那不是你的工作,那是ObjectMapper.readValue的工作。 Which is why the IOException it throws should probably just be sent on unmolested.这就是为什么它抛出的 IOException 可能应该在不受干扰的情况下发送。

Your example is nearly done.你的例子快完成了。 I changed only the throws Type to T and throw the given exception.我只将 throws Type 更改为 T 并抛出给定的异常。

public <T extends Throwable> Object mapJsonToDto(Class<?> dtoClass, String jsonDto, T exceptionToThrow) throws T {
    Object dto = null;
    try {
        dto = new ObjectMapper().readValue(jsonDto, dtoClass);
    } catch (IOException e) {
        throw exceptionToThrow;
    }
    return dto;
}

Call: mapJsonToDto(String.class, "helo", new IllegalStateException());调用: mapJsonToDto(String.class, "helo", new IllegalStateException());

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

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