「Java開発」e.printStackTrace()のコンテンツを取得するサンプルコード

Javaコード:
public static void main(String[] args) {
try {
String test_startnews24 = “";
System.out.println(test_startnews24.substring(3));
} catch (Exception e) {
e.printStackTrace();
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw, true));
String str = sw.toString();
System.out.println(“**********");
System.out.println(str);
}
}

結果
java.lang.StringIndexOutOfBoundsException: String index out of range: -3

at java.lang.String.substring(Unknown Source)

at java.lang.String.substring(Unknown Source)

at Getc.main(Getc.java:16)

**********

java.lang.StringIndexOutOfBoundsException: String index out of range: -3

at java.lang.String.substring(Unknown Source)

at java.lang.String.substring(Unknown Source)

at Getc.main(Getc.java:16)

Java

Posted by arkgame