Java中本例的方法实现

发布于 2024-08-26 01:10:32 字数 337 浏览 0 评论 0原文

我刚刚看到这样的代码片段:

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

现在我想知道静态方法 throwUncheckedException (Throwable e) 会准确返回什么以及如何在泛型方面实现它。

有人能给我举个例子吗?

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.

Can anybody give me an example ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

路还长,别太狂 2024-09-02 01:10:32

您可以像这样定义该方法:

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

这本质上意味着“对于任何类型 T,返回它”。然后,您依靠编译器猜测 T = RT 的能力。

我认为从 throwUncheckedException 方法返回值的想法如下:您想要调用一个总是抛出运行时异常的方法,并且从非 void 方法中调用它。 Java 编译器抱怨调用者在每个执行分支的末尾都需要一个 return 语句。这个技巧可以让你省去“虚拟返回”的麻烦。

You would define the method like this:

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

which essentially means "for any type T, return it". Then you rely on the compiler's ability to guess that 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. Java compiler complains that the caller needs a return statement at the end of every execution branch. This trick saves you the need for a "dummy return".

江湖彼岸 2024-09-02 01:10:32

我不得不猜测它包含:

抛出新的RuntimeException(e);

但实际上,我猜测返回值只是为了显示。该方法称为“抛出”。

如果它返回某些内容,它会返回“RT”类型的内容,无论是什么。

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'.

If it returns something, it returns something of type 'RT', whatever that is.

错々过的事 2024-09-02 01:10:32

让方法 throwUncheckedException() 返回某些内容可以避免在需要返回值的调用方法中出现额外的行。

如果异常是直接从handle方法抛出的,如下所示:

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

将不需要return语句(实际上它甚至是非法的),因为编译器将能够知道抛出之后的任何语句都是不可访问的。

但由于 throw 是在另一个方法中完成的,并且由于 handler 方法需要返回值,因此必须添加返回语句。

让 throwUncheckedException() 返回一些内容允许调用位于 return 语句中。

throwUncheckedException() 必须接近于:

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

恕我直言,这提出了你的问题这一事实表明它太令人费解了,不值得。也许实现相同目标的更好方法是这样的:

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

Having the method throwUncheckedException() returning something avoids an extra line in a calling method that needs a return value.

Had the exception been thrown directly from the handle method as follows:

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.

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.

Having the throwUncheckedException() return something allows the call to be in the return statement.

The throwUncheckedException() must be something close to:

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文