gpt4 book ai didi

java - 检查异常不会抛出并转为 RuntimeException

转载 作者:行者123 更新时间:2023-12-01 19:44:15 30 4
gpt4 key购买 nike

这里发生了什么?

为什么 IOException (RemoteException) 的检查子级会转换为 RuntimeException?

摘自here的片段

import java.rmi.RemoteException;

class Thrower {
public static void spit(final Throwable exception) {
class EvilThrower<T extends Throwable> {
@SuppressWarnings("unchecked")
private void sneakyThrow(Throwable exception) throws T {
throw (T) exception; // something interesting
}
}
new EvilThrower<RuntimeException>().sneakyThrow(exception);
}
}

public class ThrowerSample {
public static void main( String[] args ) {
Thrower.spit(new RemoteException("go unchecked!"));
}
}

最佳答案

What is happening here?

此代码绕过 Compile-Time Checking of Exceptions .

为什么它有用?

使用使用检查异常的 API 并不总是很方便。

最著名的例子之一是 JDBC 。使用jdbc总是需要开发人员处理SQLException 。但通常您不知道遇到异常时应该做什么。

或者,如果您正在使用的库,开发人员决定从 Exception 而不是 RuntimeException 继承异常。

<小时/>

为什么 IOException (RemoteException) 的检查子级会转换为 RuntimeException

它不会转换为 RuntimeException

抛出的异常是RemoteException:

Exception in thread "main" java.rmi.RemoteException: go unchecked!

该代码片段使用了 Java 泛型“技巧”。如果没有 @SuppressWarnings("unchecked") 行,编译器将生成未经检查的强制转换警告。

谜题 43

该代码片段是 Java Puzzlers: Traps, Pitfalls, and Corner Cases 的第 43 题的解决方案之一:

// Don’t do this - circumvents exception checking!
public static void sneakyThrow(Throwable t) {
Thread.currentThread().stop(t); // Deprecated!!
}

It is possible to write a method that is functionally equivalent to sneakyThrow without using any deprecated methods. In fact, there are at least two ways to do it. One of them works only in release 5.0 and later releases. Can you write such a method? It must be written in Java, not in JVM bytecode. You must not change the method after its clients are compiled. Your method doesn’t have to be perfect: It is acceptable if it can’t throw one or two subclasses of Exception.

书中使用泛型的解决方案:

// Don’t do this either - circumvents exception checking!
class TigerThrower<T extends Throwable> {
public static void sneakyThrow(Throwable t) {
new TigerThrower<Error>().sneakyThrow2(t);
}
private void sneakyThrow2(Throwable t) throws T {
throw (T) t;
}
}

书中关于此解决方案的注释:

A warning is the compiler’s way of telling you that you may be shooting yourself in the foot, and in fact you are. The unchecked cast warning tells you that the cast in question will not be checked at run time. When you get an unchecked cast warning, modify your program to eliminate it, or convince yourself that the cast cannot fail

In summary, Java’s exception checking is not enforced by the virtual machine. It is a compile-time facility designed to make it easier to write correct programs, but it can be circumvented at run time. To reduce your exposure, do not ignore compiler warnings.

<小时/>

另请参阅:

@SneakyThrows

关于java - 检查异常不会抛出并转为 RuntimeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54063062/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com