首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否将堆栈跟踪转换为字符串?

是否将堆栈跟踪转换为字符串?
EN

Stack Overflow用户
提问于 2012-12-02 05:08:58
回答 4查看 5.7K关注 0票数 15

可以在GWT中将堆栈跟踪打印到字符串吗?我认为在java.io中使用类的常用方法是行不通的,因为java.io包在客户端不可用(而Writer、PrintWriter等都在该包中)

谢谢

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-12-03 12:32:05

我不确定是否模拟了StackTraceElement,但如果是的话,您可以运行类似这样的命令

代码语言:javascript
复制
for (StackTraceElement element : exception.getStackTrace()) {
    string += element + "\n";
}
票数 13
EN

Stack Overflow用户

发布于 2012-12-03 20:41:32

下面是我用来在GWT中以String形式检索完整堆栈跟踪的方法:

代码语言:javascript
复制
private static String getMessage (Throwable throwable) {
    String ret="";
    while (throwable!=null) {
            if (throwable instanceof com.google.gwt.event.shared.UmbrellaException){
                    for (Throwable thr2 :((com.google.gwt.event.shared.UmbrellaException)throwable).getCauses()){
                            if (ret != "")
                                    ret += "\nCaused by: ";
                            ret += thr2.toString();
                            ret += "\n  at "+getMessage(thr2);
                    }
            } else if (throwable instanceof com.google.web.bindery.event.shared.UmbrellaException){
                    for (Throwable thr2 :((com.google.web.bindery.event.shared.UmbrellaException)throwable).getCauses()){
                            if (ret != "")
                                    ret += "\nCaused by: ";
                            ret += thr2.toString();
                            ret += "\n  at "+getMessage(thr2);
                    }
            } else {
                    if (ret != "")
                            ret += "\nCaused by: ";
                    ret += throwable.toString();
                    for (StackTraceElement sTE : throwable.getStackTrace())
                            ret += "\n  at "+sTE;
            }
            throwable = throwable.getCause();
    }

    return ret;
}
票数 9
EN

Stack Overflow用户

发布于 2012-12-03 22:10:25

我不建议尝试在GUI标签中显示错误堆栈跟踪。

1)它们在GWT混淆之后是不可读的。它们看起来就像是一堆换行的制表符对齐的字符。

2)非I18N格式。

3)正确的方法是向用户显示格式正确的错误“消息”。exception.getMessage()将为您提供一行非obf信息,这些信息应该为用户提供必要的UX交互。

4)如果你正在寻找对调试有帮助的well formed异常堆栈跟踪(而不是用户),你应该使用GWT模式异常的记录良好的日志特性-

a) https://developers.google.com/web-toolkit/doc/latest/DevGuideLogging

b)还可以在http://code.google.com/p/google-web-toolkit/wiki/WebModeExceptions上阅读

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13663753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档