[英]print stacktrace of runtime errors to the file
我想知道是否有一種無需使用try catch塊並將其存儲到文件中即可捕獲此機架跟蹤的方法。
我在用
if(Thread.currentThread().getStackTrace() != null){
logger.log(LogStatus.FAIL, "Rune Time Exception");
report.endTest(logger);
report.flush();
}
但這不知道是否失敗,如果堆棧跟蹤中有內容,它將進入if語句。 有沒有辦法以某種方式捕獲JUnit選項卡上的“錯誤”關鍵字? 然后將堆棧跟蹤記錄到日志文件中?
謝謝
您要查找的內容稱為默認異常處理程序。 (Java UncaughtExceptionHandler )
這是有關使用它的教程 。
//ExceptionHandlerExample.java package com.air0day.machetejuggling; public class ExceptionHandlerExample { public static void main(String[] args) throws Exception { Handler handler = new Handler(); Thread.setDefaultUncaughtExceptionHandler(handler); Thread t = new Thread(new SomeThread(), "Some Thread"); t.start(); Thread.sleep(100); throw new RuntimeException("Thrown from Main"); } } class Handler implements Thread.UncaughtExceptionHandler { public void uncaughtException(Thread t, Throwable e) { System.out.println("Throwable: " + e.getMessage()); System.out.println(t.toString()); } } class SomeThread implements Runnable { public void run() { throw new RuntimeException("Thrown From Thread"); } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.