200分 求教 如何修改throwable里面的StackTraceElement的内容 都来看看哇 很简单的问题 但是我不会
看下面的代码 我想在throwable里面的StackTraceElement[]里面 加一点东西 然后打印出来
比如我希望打印出:
java.lang.NullPointerException
class method file (0)
但是 class method file (0) 这一行怎么也打印不出来 我也不知道为什么
谁可以帮帮我哇 200分奉上
(其实就是想要在throwable里面自定义一个额外的StackTraceElement 然后打印出来)
public class ThrowableTest { public static void main(String[] args) { String[] a = null; try { System.out.println(a[2]); } catch (Throwable e) { StackTraceElement[] ste = new StackTraceElement[e.getStackTrace().length+1]; ste[0] = e.getStackTrace()[0]; ste[1] = new StackTraceElement("class", "method", "file", 0); e.setStackTrace(ste); System.out.println(e); } }}public static void main(String[] args) { String[] a = null; try { System.out.println(a[2]); } catch (Throwable e) { StackTraceElement[] ste = new StackTraceElement[2]; ste[0] = e.getStackTrace()[0]; ste[1] = new StackTraceElement("class", "method", "file", 0); e.setStackTrace(ste); System.out.println(e); System.out.println(e.getStackTrace()[1].toString()); } }