位置:首頁 > Java技術 > java.lang > java.lang.ThreadGroup.uncaughtException()方法實例

java.lang.ThreadGroup.uncaughtException()方法實例

java.lang.ThreadGroup.uncaughtException() 方法被調用的Java虛擬機時,該線程組中的線程停止,因為未捕獲的異常,線程冇有安裝特定的Thread.UncaughtExceptionHandler。

聲明

以下是java.lang.ThreadGroup.uncaughtException()方法的聲明

public void uncaughtException(Thread t, Throwable e)

參數

  • t -- 這是將要退出線程。

  • e -- 這是未捕獲的異常。

返回值

此方法不返回任何值。

異常

  • NA

例子

下麵的例子顯示java.lang.ThreadGroup.uncaughtException()方法的使用。

package com.yiibai;

import java.lang.*;

 public class ThreadGroupDemo implements Runnable {

    public static void main(String[] args) {
       ThreadGroupDemo tg = new ThreadGroupDemo();
       tg.func();
    }

    public void func()
    {
       try {
          // create a new ThreadGroup and a child for that ThreadGroup.
          newThreadGroup pGroup = new newThreadGroup("ParentThreadGroup");
          newThreadGroup cGroup = new newThreadGroup
          (pGroup,"ChildThreadGroup");

          // create another thread
          Thread thr2 = new Thread(pGroup, this);
          System.out.println("Starting " + thr2.getName() + "...");
          // this will call run() method
          thr2.start();

          // create third thread
          Thread thr3 = new Thread(cGroup, this);
          System.out.println("Starting " + thr3.getName() + "...");
          // this will call run() method
          thr3.start();

          try {
             Thread.sleep(500);
          }
          catch(InterruptedException ex) {}

          // interrupt the two threads
          thr2.interrupt();
          thr3.interrupt();

          // block until the other threads finish
          thr2.join();
          thr3.join();
        }
        catch(InterruptedException e) {
           System.out.println(e.toString());
        }
    }

    public void run() {
        try {
           System.out.print(Thread.currentThread().getName());
           System.out.println(" executing...");

           while(true) {
              Thread.sleep(500);
           }
        }
        catch(InterruptedException e) {
           Thread currThread = Thread.currentThread();
           System.out.print(currThread.getName());
           System.out.println(" interrupted:" + e.toString());

           // rethrow the exception
           throw new RuntimeException(e.getMessage());
        }
    }
}

class newThreadGroup extends ThreadGroup {
    
    newThreadGroup(String n) {
       super(n);
    }
    
    newThreadGroup(ThreadGroup parent, String n) {
       super(parent, n);
    }

    public void uncaughtException(Thread t, Throwable e) {
       System.out.println(t + " has unhandled exception:" + e);
    } 
} 

讓我們來編譯和運行上麵的程序,這將產生以下結果:

Starting Thread-0...
Starting Thread-1...
Thread-0 executing...
Thread-1 executing...
Thread-0 interrupted: java.lang.InterruptedException: sleep interrupted
Thread[Thread-0,5,ParentThreadGroup] has unhandled exception: java.lang.RuntimeException: sleep interrupted
Thread-1 interrupted: java.lang.InterruptedException: sleep interrupted
Thread[Thread-1,5,ChildThreadGroup] has unhandled exception: java.lang.RuntimeException: sleep interrupted