printStackTrace、toString、getMessage的区别

printStackTrace、toString、getMessage的区别主要在打印的详细程度依次递减:

printStackTrace打印错误输出内容样式:

Name of the Exception: Message of the Exception
Stack trace

toString打印错误输出内容样式:

Name of the Exception: Message of the Exception

getMessage打印错误输出内容样式:

Message of the Exception

可以通过下面的例子对其进行验证:

package com.toby.personal.testlistview;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

    private final static String TAG = "TAG_Test";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            int test = 10 / 0;
            Log.d(TAG, String.valueOf(test));
        } catch (Exception e) {
            Log.d(TAG, "================================Exception Start========================");
            e.printStackTrace();
            Log.d(TAG, e.getMessage());
            Log.d(TAG, e.toString());
            Log.d(TAG, "================================Exception End==========================");
        }

    }
}

运行之后控制台的log输出截图:

printStackTrace、toString、getMessage的区别_第1张图片
log输出截图

参考文章:
https://gopalakrishnadurga.wordpress.com/2012/05/09/printstacktracetostringgetmessage/

你可能感兴趣的:(printStackTrace、toString、getMessage的区别)