In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How Android catches java anomalies is not very clear to many beginners. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.
1. Global capture of java exception
For java exception global capture code:
Val default = Thread.getDefaultUncaughtExceptionHandler () Thread.setDefaultUncaughtExceptionHandler {t, e-> / / handle exception Log.e ("Uncaught", "exception message:" + e.message) / / return the exception to the original registered handler default.uncaughtException (t, e)}
The above is a very simple piece of code, which is often used to catch java exceptions globally, but my question is how he achieves global capture. With this doubt, let's take a look at the code.
Let's take a look at who called the static method getDefaultUncaughtExceptionHandler and look at all the classes called by the class. Only ThreadGroup is the most reliable:
When parent is empty, getDefaultUncaughtExceptionHandler will be called to call back the exception, and then continue to find out who triggered the uncaughtException of ThreadGroup. There is no reliable circle. While I was hesitating, I glanced at the notes by the way and found miraculously:
-Called by the Java Virtual Machine when a thread in this- thread group stops because of an uncaught exception, and the thread- does not have a specific {[@ link] (/ link%20) Thread.UncaughtExceptionHandler}-installed.
This method is called by JVM when an uncaught exception causes threads in the thread group to stop. Then let's search the source code of jvm to see how this method is triggered.
The JavaThread::exit method in the thread.cpp of the Hotspot virtual machine source code finds a piece of code like this and also gives a comment:
When the thread calls exit exit, if there is an uncaught exception, the Thread.dispatchUncaughtException method is called, and we continue to trace the method:
Then call the uncaughtException distribution exception of the current thread:
Interestingly, if we do not set UncaughtExceptionHandler for the current thread, we will leave this exception to the current thread's ThreadGroup handling. If we set UncaughtExceptionHandler for the current thread, the current thread has an exception that will never be thrown to getDefaultUncaughtExceptionHandler, which is suitable for catching current thread exceptions.
Finally, we go back to the ThreadGroup.UncaughtExceptionHandler method we saw at first, and paste it back to the original image to continue the analysis:
This place will continue to determine whether the parent is empty. Parent is a ThreadGroup,ThreadGroup that implements the Thread.UncaughtExceptionHandler interface. I'll just say the answer here. The relationship between ThreadGroup and Thread will eventually go to the parent of system's ThreadGroup,system is empty. At this time, go to the else branch, get the getDefaultUncaughtExceptionHandler static variable in Thread, and trigger the uncaughtException method. Because we set this static variable in Activity, we received this exception notification.
Second, a little knowledge
1. How to catch an exception without exiting val default = Thread.getDefaultUncaughtExceptionHandler () Log.e ("Uncaught", "Uncaught handler:" + default) / / Uncaught handler: com.android.internal.os.RuntimeInit$KillApplicationHandler@21f02a3Thread.setDefaultUncaughtExceptionHandler {t, e-> / / return the exception to the original registered handler / / default.uncaughtException (t, e)}
After catching an exception, nothing is handled. However, doing so is very unorthodox, which will cause other frameworks to fail to catch exception escalation through the previously set static variables. I printed that default is RuntimeInit, and this class will do killProcess when it catches an exception.
2. How to catch the relationship structure of specified thread exception val thread = Thread {val a = 1ap0} thread.setUncaughtExceptionHandler {t, e-> Log.e ("Uncaught", "Uncaught trace:" + e.message)} thread.start () 3, ThreadGroup and Thread
The parent of Thread is specified during new Thread, and a custom ThreadGroup is constructed. The default is to use the ThreadGroup that creates the current thread.
The timing when Thread is added to ThreadGroup's Thread [] array is done when calling start to start the thread.
The parent of ThreadGroup is specified during new ThreadGroup, and a custom ThreadGroup is constructed. The default is to use the ThreadGroup of the current thread.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.