为什么 java 编译器抛出“RemoteException 无法访问的 catch 块”。在 try-catch 中出现 RemoteException?

问题描述 投票:0回答:1

我有这个代码:

public static void main(String[] args) {

         
        try {
            
            Registry registro = LocateRegistry.getRegistry();
            
            EchoInt stub = (EchoInt) UnicastRemoteObject.exportObject(new EchoObjectRMI(), 0);
            // bind (or rebind) the stub into the local registry
            registro.rebind("echoRemote", stub);
        } catch (RemoteException e) {
            System.err.println("Something wrong happended on the remote end");
            e.printStackTrace();
            System.exit(-1); // can't just return, rmi threads may not exit
        }
        System.out.println("The echo server is ready");
    }
}

但是当我尝试运行它时,编译器会抛出此错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Unreachable catch block for RemoteException. This exception is never thrown from the try statement body

    at server.EchoObjectRMI.main(EchoObjectRMI.java:33)

我不明白为什么。理论上,exportObject方法会抛出RemoteException异常,那么为什么会说“这个异常永远不会从try语句体中抛出”呢?

java compiler-errors rmi
1个回答
0
投票

您的代码不正确,因此不应编译。

一些编译器可以通过跳过错误来编译语法不正确的代码,希望这部分不会被调用。如果执行的话,行为是不可预测的。

© www.soinside.com 2019 - 2024. All rights reserved.