简体   繁体   English

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

[英]print stacktrace of runtime errors to the file

I have Junit test where sometimes it fails due to runtime errors 我进行了Junit测试,有时由于运行时错误而失败 故障跟踪屏幕

I was wondering if there is a way to capture this srack trace without using try catch block and storing it into a file. 我想知道是否有一种无需使用try catch块并将其存储到文件中即可捕获此机架跟踪的方法。

I was using 我在用

        if(Thread.currentThread().getStackTrace() != null){

           logger.log(LogStatus.FAIL, "Rune Time Exception");
           report.endTest(logger);
           report.flush();
       }

but this does not know if there was a failure or not, it goes into the if statement if there is a something in the stack trace. 但这不知道是否失败,如果堆栈跟踪中有内容,它将进入if语句。 Is there a way to somehow capture the the "Errors" keyword on a JUnit tab? 有没有办法以某种方式捕获JUnit选项卡上的“错误”关键字? and then log the stack trace into the log file? 然后将堆栈跟踪记录到日志文件中?

Thank you 谢谢

What you are looking for is called a default exception handler. 您要查找的内容称为默认异常处理程序。 (Java UncaughtExceptionHandler ) (Java UncaughtExceptionHandler

Here is a tutorial about using it. 这是有关使用它的教程

 //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