Home arrow static arrow Java Programming [Archive] - Catch exceptions out of catch block
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Catch exceptions out of catch block
This topic has 2 replies on 1 page.

Posts:3
Registered: 8/9/04
Catch exceptions out of catch block  
Aug 9, 2004 5:48 AM



 
Hello,

Here in my job we was using Log4J , but since some of our logs need complex things like replication for
some databases analise log t see where we will send in and lots of emails too , so we are developing our own log framework, my question is , there is a way to caught exceptions not declared in a try catch block?

Example: IndexOutOfBound some time are not cought in a try catch block, or some files sometimes came null and throws uncauht exceptions some times, there is a way to me listen to all execptions wich was throwed to the system and have a no catch block??

Thank You

Ricardo

 

Posts:24,036
Registered: 2/3/03
Re: Catch exceptions out of catch block  
Aug 9, 2004 5:49 AM (reply 1 of 2)



 
You can catch the specific RuntimeException, the RuntimException superclass, or Throwable.
 

Posts:271
Registered: 08.07.99
Re: Catch exceptions out of catch block  
Aug 9, 2004 6:17 AM (reply 2 of 2)



 
When an uncaught exception occurs, the JAVA runtime system determins the ThreadGroup of the current thread and calls "uncaughtException" on that ThreadGroup object. However, the java.lang.ThreadGroup object performs the printing of the stack trace, which you will normally see in such cases.

If you want to modify this behaviour, you could write your own ThreadGroup class, which extends java.lang.ThreadGroup and provides a different implementation of uncaughtException. E.g. you could write a log message to your own log file or whatever you want.

Now you must guarantee, that all threads in your system are childs of that customized ThreadGroup. I did this in my main class:
public class Application implements Runnable {    private String[] argv;     public static void main(String[] argv) {        this.argv = argv; // save this for use in the run method        ThreadGroup tg = new MyThreadGroup();        // start a thread in the new thread group and terminate the original main thread        new Thread(tg, this, "main").start();    }     public void run() {        // perform all other tasks here    }}public class MyThreadGroup extends ThreadGroup {    public MyThreadGroup()  {        super("MyThreadGroup");    }    public void uncaughtException(Thread thread, Throwable exception) {        // ignore death of thread        if (exception instanceof ThreadDeath)        {            return;        }         // ---- perform your actions here ----    }} 

All other threads, which are created later, are also in the new thread group, because they inherit the group from their creator.
 
This topic has 2 replies on 1 page.