简体   繁体   English

Java中这种情况的方法实现

[英]method implementation for this case in Java

I just saw a code snippet like this: 我刚看到这样的代码片段:

private static class DefaultErrorHandler<RT> implements ErrorHandler<RT> {
  public RT handle(Object[] params, Throwable e) {
   return Exceptions.throwUncheckedException(e);
  }
}

Now I am wondering what the static method throwUncheckedException (Throwable e) would return exactly and how it might be implemented regarding the generics. 现在我想知道静态方法throwUncheckedException (Throwable e)将准确返回什么以及如何实现泛型。

Can anybody give me an example ? 谁能举个例子?

You would define the method like this: 您可以像这样定义方法:

public static <T> T throwUncheckedException (Throwable e) { ... }

which essentially means "for any type T, return it". 这基本上意味着“对于任何类型的T,返回它”。 Then you rely on the compiler's ability to guess that T = RT. 然后你依靠编译器猜测 T = RT 的能力

I think the idea of returning a value from the throwUncheckedException method is as follows: you want to call a method that always throws a run-time exception, and you call it from a non-void method. 我认为从throwUncheckedException方法返回值的想法如下:您希望调用始终抛出运行时异常的方法,并从非void方法调用它。 Java compiler complains that the caller needs a return statement at the end of every execution branch. Java编译器抱怨调用者在每个执行分支的末尾都需要一个return语句。 This trick saves you the need for a "dummy return". 这个技巧可以节省你“虚拟回归”的需要。

I would have to guess that it consists of : 我不得不猜测它包括:

throw new RuntimeException(e);

but really, I'm guessing that the return value is just for show. 但实际上,我猜测返回值仅供展示。 The method is called 'throw'. 该方法称为“throw”。

If it returns something, it returns something of type 'RT', whatever that is. 如果它返回某些东西,它会返回“RT”类型的东西,无论是什么。

Having the method throwUncheckedException() returning something avoids an extra line in a calling method that needs a return value. 使用throwUncheckedException()方法返回一些东西可以避免调用方法中需要返回值的额外行。

Had the exception been thrown directly from the handle method as follows: 如果异常直接从handle方法抛出,如下所示:

public RT handle(Object[] params, Throwable e) {
    throw new RuntimeException(e);
}

The return statement would not have been needed (it would even have been illegal actually) because the compiler would be able to know that any statement following the throw would be unreachable. 不需要return语句(实际上它甚至可能是非法的),因为编译器将能够知道throw之后的任何语句都是无法访问的。

But since the throw is done in another method, and since a return value is required for the handle method, a return statememt must be added. 但由于throw是在另一个方法中完成的,并且由于handle方法需要返回值,因此必须添加return statememt。

Having the throwUncheckedException() return something allows the call to be in the return statement. 让throwUncheckedException()返回一些东西允许调用在return语句中。

The throwUncheckedException() must be something close to: throwUncheckedException()必须接近:

public static <T> T throwUncheckedException(Throwable e) {
    throw new RuntimeException(e);
    return null;
}

IMHO, the fact that this raised your question shows that it is too puzzling to be worthy. 恕我直言,事实上,这提出了你的问题,这表明它太令人费解了。 Perhaps a better way to achieve the same goal would be something like this: 也许更好的方法来实现相同的目标是这样的:

public RT handle(Object[] params, Throwable e) {
    throw Exceptions.makeUncheckedException(e);
}

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

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