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

  • Art Food Kitty - Kelly Eddington

    Art Food Kit

    7 Kasım 2006
  • o0oCyrusViruso0o

    o0oCyrusViru

    11 Mart 2008
  • ŠĩŗĜŕôŵåɭȍҭҭ

    ŠĩŗĜŕô

    29 Kasım 2009