0
1

More than 1 year has passed since last update.

e.printStackTrace()とSystem.out.println(e)とSystem.out.println(e.getMessage())の違い

Last updated at Posted at 2023-01-24

e.printStackTrace()

System.out.println(e)

System.out.println(e.getMessage())
の違い。

public class App {
    public static void main(String[] args) throws Exception {

        String aaa = "aaa";

        try{
            aaa.substring(1000);
        }catch(Exception e){
            e.printStackTrace();
            System.out.println(e);
            System.out.println(e.getMessage());
        }
    }
}

e.printStackTrace()の場合

java.lang.StringIndexOutOfBoundsException: begin 1000, end 3, length 3
        at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4604)
        at java.base/java.lang.String.substring(String.java:2707)
        at java.base/java.lang.String.substring(String.java:2680)
        at App.main(App.java:8)

System.out.println(e)の場合

java.lang.StringIndexOutOfBoundsException: begin 1000, end 3, length 3

※System.out.println(e.fillInStackTrace())も同じ結果になる。

System.out.println(e.getMessage())の場合

begin 1000, end 3, length 3
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1