2014-06-09 2 views
2

내 멀티 스레드 응용 프로그램 중 하나가 예외를 throw 할 때 스레드 이름을 포함하지 않습니다. 아래처럼 예외가 발생합니다스레드 이름이없는 예외

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 
    at java.util.ArrayList.rangeCheck(Unknown Source) 
    at java.util.ArrayList.get(Unknown Source) 
    at dataimporter.Importer.run(Importer.java:201) 
    at java.lang.Thread.run(Unknown Source) 

어쨌든 스레드 이름을 포함할까요?

+1

'201 '행의'Importer.java' 클래스에있는 명령문은 빈'ArrayList'의 첫 번째 요소에 접근하려고 시도합니다. –

+0

@ PM77-1 예 알아요,하지만 어떻게 스레드 이름을 포함시킬 수 있습니까? – Arya

+0

이미 [이 기사] (http://www.drdobbs.com/jvm/uncaught-java-thread-exceptions/240148320)를 찾았습니까? –

답변

4

UncaughtExceptionHandlerThread에 등록하고 필요한 정보를 인쇄 할 수 있습니다.

Thread thread = new Thread(..); 
thread.setUncaughtExceptionHandler((t, ex) -> // fancy Java 8 syntax 
    System.out.println(t.getName() + " " + ex) 
); // or print the stack trace after it 
thread.start(); 

위의 코드에는 Java 8 구문이 있습니다. 사용하지 않는 경우 익명의 클래스 만 사용하면됩니다.

UncaughtExceptionHandler handler = new UncaughtExceptionHandler() { 
    @Override 
    public void uncaughtException(Thread t, Throwable ex) { 
     System.out.println(t.getName() + " " + ex);    
    } 
}; 
thread.setUncaughtExceptionHandler(handler); 
+0

+1 그러나 OP가 Java 8을 사용하고 있음을 어떻게 알 수 있습니까? – hexafraction

+0

@ hexafraction 전 do not :(. –