繁体   English   中英

将运行时错误的stacktrace打印到文件

[英]print stacktrace of runtime errors to the file

我进行了Junit测试,有时由于运行时错误而失败 故障跟踪屏幕

我想知道是否有一种无需使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM