CODE: Utiltiy class to intercept uncaught exceptions and convert them to events.

S

Sandip Chitale

Here is the output from the test:
java -classpath . UncaughtExceptionInterceptorTest

Thread[UncaughtExceptionInterceptorThread,5,UncaughtExceptionInterceptorThre
adGroup]
java.lang.RuntimeException: Uncaught Exception Interceptor Test

Here is the code:

import java.util.Vector;
import java.util.EventObject;

/**
* The java.lang.ThreadGroup has an ability to catch any uncaught exception.
* This utility class inserts a ThreadGroup and convert these uncaught
exception
* to events.
*
* Typical usage:
*
* <pre>
* public class UncaughtExceptionInterceptorTest {
* public static void main(String[] args) {
* MyUncaughtExceptionInterceptor muei = new
MyUncaughtExceptionInterceptor();
* muei.addUncaughtExceptionListener(new
UncaughtExceptionInterceptor.UncaughtExceptionListener() {
* public void
uncaughtException(UncaughtExceptionInterceptor.UncaughtExceptionEvent
ueEvent) {
* System.out.println(ueEvent.getThread());
* System.out.println(ueEvent.getThrowable());
*
* }
* });
* muei.interceptUncaughtExceptions(args);
* }
*
* public static void safeMain(String[] args) {
* throw new RuntimeException("Uncaught Exception Interceptor
Test");
* }
*
* private static class MyUncaughtExceptionInterceptor extends
UncaughtExceptionInterceptor
* {
* public void main(String[] args) {
* safeMain(args);
* }
* }
* }
* </pre>
*/
public abstract class UncaughtExceptionInterceptor {
private String _threadGroupName;
private String _threadName;

private Vector _listeners;

public UncaughtExceptionInterceptor() {
this(UncaughtExceptionInterceptor.class.getName());
}

public UncaughtExceptionInterceptor(String name) {
_threadGroupName = name + "ThreadGroup";
_threadName = name + "Thread";

_listeners = new Vector();
}

/**
* Override this method for callback.
*/
public abstract void main(String[] args);

/**
* Add listener.
*/
public void addUncaughtExceptionListener(UncaughtExceptionListener
listener) {
_listeners.addElement(listener);
}

/**
* Remove listener.
*/
public void removeUncaughtExceptionListener(UncaughtExceptionListener
listener) {
_listeners.removeElement(listener);
}

void fireUncaughtExceptionEvent(Thread t, Throwable e) {
UncaughtExceptionEvent ueEvent = new UncaughtExceptionEvent(this, t,e);
int size = _listeners.size();
for (int i = 0; i < size; i++) {
UncaughtExceptionListener aListener =
(UncaughtExceptionListener)_listeners.elementAt(i);
aListener.uncaughtException(ueEvent);
}
}

public void interceptUncaughtExceptions(final String[] args) {
ThreadGroup tg = new
ThreadGroup(Thread.currentThread().getThreadGroup(),_threadGroupName)
{
public void uncaughtException(Thread t, Throwable e) {
fireUncaughtExceptionEvent(t, e);
}
};
Thread t = new Thread(tg, _threadName) {
public void run() {
// call overridden main
main(args);
}
};
//t.setDaemon(true);
t.start();
try {
t.join();
} catch (InterruptedException e) {
// broadcast execption
fireUncaughtExceptionEvent(Thread.currentThread(), e);
}
}

/**
* Uncaught Exception Event
*/
public static class UncaughtExceptionEvent extends EventObject {
private Thread _thread;
private Throwable _throwable;


public UncaughtExceptionEvent(Object source, Thread thread, Throwable
throwable) {
super(source);
_thread = thread;
_throwable = throwable;
}

/**
* Gets the value of thread
*
* @return the value of thread
*/
public Thread getThread() {
return this._thread;
}

/**
* Gets the value of throwable
*
* @return the value of throwable
*/
public Throwable getThrowable() {
return this._throwable;
}
}

/**
* Listener of file open rquests.
*/
public static interface UncaughtExceptionListener {
void uncaughtException(UncaughtExceptionEvent event);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top