Deal with the anomalies to be arrested
In our procedure is not all the anomalies have been arrested, some because we do not want to catch, because we believe that there was no need to capture those anomalies
And some is that we can not capture, because we can not be sure what they will be in place, such as NullPointerException, ClassCastException, IndexOutOfBoundsException these RuntimeException. Perhaps we can all they have the potential to capture their place, but it is a very bad solution. However, in some circumstances, we must not caught in some time to do some unusual things, such as the release of resources to enable the process to leave the wrong state, and so on.
ThreadGroup adopted here to provide a method to resolve this problem so that we can process more robust. ThreadGroup uncaughtException provide a callback method, when the threads in the thread group has not yet caught abnormal, JVM, will call this method.
Public class ApplicationLoader extends ThreadGroup (
Private ApplicationLoader () (
Super ( "ApplicationLoader");
)
Public static void main (String args []) (
Runnable addStarter = new Runnable () (
Public void run () (
/ / Here call our own procedures for the import function
/ / MyApplication.main (args);
)
);
/ / Our own program as a group this thread to run a thread
New Thread (new ApplicationLoader (), addStarter). Start ();
)
/ * When not capture the lead to abnormal thread stop, this method will be called virtual machine, as long as we in their own sub-category ThreadGroup override this method, in which the anomalies corresponding * /
Public void uncaughtException (Thread threadf, Throwable e) (
/ / Handle the Exception
)
)








0 Comments to “Deal with the anomalies to be arrested”
No Comments. Send your comment.
Leave a Reply
You must be logged in to post a comment.