本文介绍了使用stacktrace从另一个异常初始化异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有客户端 - 服务器系统。它们通过RMI进行通信,因此涉及序列化/反序列化。服务器根据请求向客户端发送响应。如果发生异常,它会在响应中设置。

I have client-server system. They communicate via RMI, so serialization/deserialization is involved. Server sends a response to client upon a request. If exception occurs it is set in the response.

但是,如果在服务器上发生异常,客户端不知道它。所以我需要包装原始异常,但保留stacktrace调试目的。是否有更优雅的解决方案?

However, if some exception occurs at the server and the client does not know about it. So I need to wrap the original exception but to keep the stacktrace for debug purposes. Is there more elegant solution?

//response from server to client
class Response {
    private MyException e;

    public void set(MyException e) {
       this.e = e;
    }
}

//some other code
catch (MyException e) {
   response.set(e);      
} catch (Exception e) {
   //could be exception which does not exist at client
   //so I can not just set because it would cause ClassNotFoundException at client
   response.set(new MyException(e.getMessage() + ": " + e.getStackTrace()));   
}

似乎API只支持设置 code>这是我不能因为上面提到的问题。我们可以设置一个stacktrace从另一个异常,而不设置原因?

It seems that API supports only setting cause which I cannot because of the problem abovementioned. Can I set a stacktrace from another exception without setting cause?

推荐答案

你可以考虑方法,例如:

You might consider the method setStackTrace(), for example:

Throwable originalException = ...;
Throwable clientException = ...;
clientException.setStackTrace(originalException.getStackTrace());

stacktrace元素本身似乎只包含字符串,因此这不应该对客户端它不知道特殊的服务器类。

The stacktrace elements themselves appear to consist of strings only, so this should not present a problem for the client which does not know about special server classes.

这篇关于使用stacktrace从另一个异常初始化异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 19:14