java RMI 和异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/612520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:04:10  来源:igfitidea点击:

RMI and exceptions

javanetworkingrmi

提问by Malachi

I am new to using RMI and I am relatively new to using exceptions.

我是使用 RMI 的新手,我对使用异常也比较陌生。

I want to be able to throw an exception over RMI (is this possible?)

我希望能够通过 RMI 抛出异常(这可能吗?)

I have a simple server which serves up students and I have delete method which if the student doesn't exist I want to throw a custom exception of StudentNotFoundException which extends RemoteException (is this a good thing to do?)

我有一个简单的服务器,它为学生提供服务,我有删除方法,如果学生不存在,我想抛出一个扩展 RemoteException 的 StudentNotFoundException 的自定义异常(这是一件好事吗?)

Any advice or guidance would be greatly appreciated.

任何建议或指导将不胜感激。

Server Interface method

服务器接口方法

    /**
 * Delete a student on the server
 * 
 * @param id of the student
 * @throws RemoteException
 * @throws StudentNotFoundException when a student is not found in the system
 */
void removeStudent(int id) throws RemoteException, StudentNotFoundException;

Server method implementation

服务器方法实现

    @Override
public void removeStudent(int id) throws RemoteException, StudentNotFoundException
{
    Student student = studentList.remove(id);

    if (student == null)
    {
        throw new StudentNotFoundException("Student with id:" + id + " not found in the system");
    }
}

Client method

客户端方法

    private void removeStudent(int id) throws RemoteException
{
    try
    {
        server.removeStudent(id);
        System.out.println("Removed student with id: " + id);
    }
    catch (StudentNotFoundException e)
    {
        System.out.println(e.getMessage());
    }

}

StudentNotFoundException

学生未找到异常

package studentserver.common;

import java.rmi.RemoteException;

public class StudentNotFoundException extends RemoteException
{
    private static final long serialVersionUID = 1L;

    public StudentNotFoundException(String message)
    {
        super(message);
    }
}

Thank you for your reply I have now managed to fix my problem and realised that extending RemoteException was bad idea.

感谢您的回复,我现在已经设法解决了我的问题,并意识到扩展 RemoteException 是个坏主意。

采纳答案by Outlaw Programmer

It's OK to throw any kind of exception (even custom ones), just make sure to package them up in your export .jar file (if you're using a version of Java where you need to do this manually).

抛出任何类型的异常(甚至是自定义异常)都可以,只需确保将它们打包在导出的 .jar 文件中(如果您使用的是需要手动执行此操作的 Java 版本)。

I wouldn't subclass RemoteException, though. Those are typically thrown if there is some kind of connection problem. Presumably, your client will handle connection problems differently from other types of problems. You might tell the user the server is down when you catch a RemoteException, or connect to a different server. For StudentNotFoundException, you probably want to give the user another chance at entering the student info.

不过,我不会将 RemoteException 子类化。如果存在某种连接问题,通常会抛出这些问题。据推测,您的客户端处理连接问题的方式与其他类型的问题不同。当您捕获 RemoteException 或连接到不同的服务器时,您可能会告诉用户服务器已关闭。对于 StudentNotFoundException,您可能希望给用户另一个输入学生信息的机会。

回答by erickson

Yes, it's possible to throw exceptions via RMI.

是的,可以通过 RMI 抛出异常。

No, it's not a good idea to extend RemoteExceptionto report application failures. RemoteExceptionsignals a failure in the remoting mechanism, like a network failure. Use an appropriate exception, extending java.lang.Exceptionyourself if necessary.

不,扩展RemoteException以报告应用程序故障不是一个好主意。RemoteException表示远程处理机制中的故障,如网络故障。使用适当的异常,java.lang.Exception必要时扩展自己。

For a more detailed explanation, look at another of my answers. In a nutshell, be careful about chaining exceptions when using RMI.

有关更详细的解释,请查看我的另一个答案。简而言之,在使用 RMI 时要小心链接异常。

回答by banjollity

I want to be able to throw an exception over RMI (is this possible?)

我希望能够通过 RMI 抛出异常(这可能吗?)

Yes. Anything can be serialized, even exceptions. I think Exception itself implements Serializable.

是的。任何东西都可以序列化,甚至是异常。我认为 Exception 本身实现了 Serializable。

I have a simple server which serves up students and I have delete method which if the student doesn't exist I want to throw a custom exception of StudentNotFoundException which extends RemoteException (is this a good thing to do?)

我有一个简单的服务器,它为学生提供服务,我有删除方法,如果学生不存在,我想抛出一个扩展 RemoteException 的 StudentNotFoundException 的自定义异常(这是一件好事吗?)

I would have it extend Exception personally. Your exceptions are your exceptions, and RemoteExceptions are for things that go wrong with RMI for connectivity reasons.

我会让它亲自扩展 Exception 。您的例外就是您的例外,而 RemoteExceptions 是针对 RMI 由于连接原因而出错的情况。

回答by Tom Hawtin - tackline

There is no need for your exceptions to extend RemoteException.

您的异常不需要扩展RemoteException

(It's worth noting that concrete exception types thrown need to be in codebases used by both the server and the client.)

(值得注意的是,抛出的具体异常类型需要在服务器和客户端使用的代码库中。)