Turn a stack trace into a string?

后端 未结 4 771
日久生厌
日久生厌 2021-01-08 00:23

Is it possible to print a stack trace to a string in GWT? The usual methods of using the classes in java.io won\'t work I think, because the java.io package is not available

4条回答
  •  执笔经年
    2021-01-08 00:42

    Here is the method I'm using to retrieve a full stack trace as a String in GWT :

    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;
    }
    

提交回复
热议问题