SORU
1 Temmuz 2011, Cuma


Nasıl bir iş parçacığı bir özel Durum yakalamak için

Java ana sınıf var, sınıf, yeni bir konu başlatmak, ana, iplik ölene kadar bekler. Bazı an, konu çalışma zamanı bir istisna atar, ancak bu durum ana sınıfı iplik atılan yakalayamıyorum.

İşte kod:

public class Test extends Thread
{
  public static void main(String[] args) throws InterruptedException
  {
    Test t = new Test();

    try
    {
      t.start();
      t.join();
    }
    catch(RuntimeException e)
    {
      System.out.println("** RuntimeException from main");
    }

    System.out.println("Main stoped");
  }

  @Override
  public void run()
  {
    try
    {
      while(true)
      {
        System.out.println("** Started");

        sleep(2000);

        throw new RuntimeException("exception from thread");
      }
    }
    catch (RuntimeException e)
    {
      System.out.println("** RuntimeException from thread");

      throw e;
    } 
    catch (InterruptedException e)
    {

    }
  }
}

Kimse nedenini bilmiyor?

CEVAP
1 Temmuz 2011, Cuma


Thread.UncaughtExceptionHandler kullanın.

Thread.UncaughtExceptionHandler h = new Thread.UncaughtExceptionHandler() {
    public void uncaughtException(Thread th, Throwable ex) {
        System.out.println("Uncaught exception: "   ex);
    }
};
Thread t = new Thread() {
    public void run() {
        System.out.println("Sleeping ...");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            System.out.println("Interrupted.");
        }
        System.out.println("Throwing exception ...");
        throw new RuntimeException();
    }
};
t.setUncaughtExceptionHandler(h);
t.start();

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Bach Vlogs

    Bach Vlogs

    18 HAZİRAN 2011
  • Caramthros

    Caramthros

    10 AĞUSTOS 2007
  • SolidWorksTutoriels

    SolidWorksTu

    14 Kasım 2013