Magical new Exception ()

Look at the next section of code:

public class MainActivity extends AppCompatActivity {

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

    private void test1() {
        test2();
    }

    private void test2() {
        test3();
    }

    private void test3() {
        test4();
    }

    private void test4() {
        Log.d("wxl", "test", new Exception());
    }
}

Can you guess what will print?

12-29 12:16:05.479 8140-8140/com.wuxiaolong.myapplication D/wxl: test
    java.lang.Exception
        at com.wuxiaolong.myapplication.MainActivity.test4(MainActivity.java:29)
        at com.wuxiaolong.myapplication.MainActivity.test3(MainActivity.java:25)
        at com.wuxiaolong.myapplication.MainActivity.test2(MainActivity.java:21)
        at com.wuxiaolong.myapplication.MainActivity.test1(MainActivity.java:17)
        at com.wuxiaolong.myapplication.MainActivity.onCreate(MainActivity.java:13)
        at android.app.Activity.performCreate(Activity.java:6975)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Is not it amazing, actually test4 () method call relations to print out, is how to do? This is not a place to put relations with each step down to save.

Log advanced first look Log.d ():

public static int d(String tag, String msg, Throwable tr) {
    return println(LOG_ID_MAIN, DEBUG, tag, msg + '\n' + getStackTraceString(tr));
}

public static String getStackTraceString(Throwable tr) {
    if (tr == null) {
        return "";
    }
    // This is to reduce the amount of log spew that apps do in the non-error
    // condition of the network being unavailable.
    Throwable t = tr;
    while (t != null) {
        if (t instanceof UnknownHostException) {
            return "";
        }
        t = t.getCause();
    }
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    tr.printStackTrace(pw);
    pw.flush();
    return sw.toString();
}

Here Exception third parameter is a subclass of Throwable, see Throwable constructor initializes:

public Throwable(String message) {
    fillInStackTrace();
    detailMessage = message;
}

/**
 * Fills in the execution stack trace. This method records within this
 * {@code Throwable} object information about the current state of
 * the stack frames for the current thread.
 *
 * <p>If the stack trace of this {@code Throwable} {@linkplain
 * Throwable#Throwable(String, Throwable, boolean, boolean) is not
 * writable}, calling this method has no effect.
 *
 * @return  a reference to this {@code Throwable} instance.
 * @see     java.lang.Throwable#printStackTrace()
 */
public synchronized Throwable fillInStackTrace() {
    if (stackTrace != null ||
        backtrace != null /* Out of protocol state */ ) {
        backtrace = nativeFillInStackTrace();
        stackTrace = libcore.util.EmptyArray.STACK_TRACE_ELEMENT;
    }
    return this;
}

Like he wants, here fillInStackTrace (), it is step by step method of tracking calls, until the track to the end of the thread. fillInStackTrace is a native method, not with the down.

Exception that can be programmed to capture and handle exception error, JVM call stack with a way to track each thread in a series of method invocation, the stack holds local information about each method call.

For stand-alone Java program that has been to the main method of the program, when a new method is called, JVM stack structure of the description of the method into the stack at the top has the correct approach to the implementation of the method, when a normal Java method is finished, JVM will be shot at from the stack structure of the method call stack, and a method for processing before proceeding, if java method throws an exception in the process of executing code, JVM must find the catch block of code can catch exceptions it first checks to see whether the current method there is a catch block, if there is to execute the catch block, otherwise the JVM call stack shot in the back of the stack structure of the method, continue to the previous method to find the appropriate catch block, If the JVM finally catch up the main () method, which is an exception has been thrown to the main () method, the exception handling code block is still not found, the thread will be abnormally terminated, if the thread is the main thread, the application also will be terminated at this time JVM will throw an exception direct user, on the user terminal will see the raw exceptions.

My public number: Wu Xiaolong students, welcome attention to communication, public key No. reply "1024" have a surprise for you.

Guess you like

Origin www.cnblogs.com/WuXiaolong/p/12130952.html