Handling unusually captured

zhaozj2021-02-16  84

Not all exceptions in our program are captured, some because we don't want to catch, because we think those exceptions do not have to capture

Some are what we can't capture, because we can't determine where they will appear, such as NullPointersException, ClassCastException, indexoutofboundsexception these runtimeException. We may be able to capture them in all where they may happen, but this is really a bad solution. However, in some cases, we must do something when there are some unprecised exceptions, like releasing resources, let the program leave incorrect state, and so on.

Here, by a method provided by ThreadGroup, solves this problem, making our programs more robust. ThreadGroup provides a UncaughTexception callback method. When the thread in the thread group has an exception that is not captured, the 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 () {

/ / Call us here's entry function //myApplication.main (args);

}

}; // Take our own procedure as a thread of this thread group to run

New Thread (New ApplicationLoader (), AddStarter) .Start ();

}

/ * When there is an unusually captured exception causes the thread to stop, this method will be called by the virtual machine, and we only need to process the abnormality in it in our own ThreadGroup subclass.

Public void uncaughtexception (Thread Threadf, throwable e) {

// Handle The Exception

}

}

转载请注明原文地址:https://www.9cbs.com/read-13124.html

New Post(0)