将异常转换为JSON

将异常转换为JSON,第1张

将异常转换为JSON

从理论上讲,您还可以遍历堆栈跟踪中的元素并生成如下内容:

{ "NullPointerException" :    { "Exception in thread "main" java.lang.NullPointerException",        {"Book.java:16" : "com.example.myproject.Book.getTitle",          "Author.java:25" : "at com.example.myproject.Author.getBookTitles",          "Bootstrap.java:14" : "at com.example.myproject.Bootstrap.main()"        }    },  "Caused By" :    { "Exception in thread "main" java.lang.NullPointerException",        {"Book.java:16" : "com.example.myproject.Book.getTitle",          "Author.java:25" : "at com.example.myproject.Author.getBookTitles",          "Bootstrap.java:14" : "at com.example.myproject.Bootstrap.main()"        }    }}

您可以像这样遍历异常:

catch (Exception cause) {    StackTraceElement elements[] = cause.getStackTrace();    for (int i = 0, n = elements.length; i < n; i++) {    System.err.println(elements[i].getFileName() + ":" + elements[i].getLineNumber()  + ">> " + elements[i].getMethodName() + "()");    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5002620.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存