我创建的java项目将针对1800个案例进行测试,每个案例的输出必须与黄金(所需)输出相匹配。我为此创建了一个perl脚本并在cygwin上运行它。
有少数情况会抛出异常,但错误地认为它们是正确的。我想在java代码中添加一个try catch块,这样如果抛出任何异常,就会捕获它,并在文件exception.txt
上打印堆栈跟踪。
Pseudo Java code:
main()
{
try
{
... //complete code of main()
}
catch (Exception e)
{
FileWriter fstream=new FileWriter("exception.txt");
BufferedWriter out=new BufferedWriter(fstream);
out.write(e.toString());
out.close();
}
}
但是这会覆盖以前的文件内容,最后文件包含最后抛出的异常。如何编写catch块以便打印stackTrace并且文件内容完好无损且每次都不会被覆盖。
答案 0 :(得分:22)
请改用此构造函数:
new FileWriter ("exception.txt", true);
描述here。
编辑:根据Jon的评论如下:
如果要打印整个堆栈跟踪,请使用printStackTrace
:
fw = new FileWriter ("exception.txt", true);
pw = new PrintWriter (fw);
e.printStackTrace (pw);
此外,请在此之后使用相应的close
来电。
答案 1 :(得分:10)
您可以使用:
FileOutputStream fos = new FileOutputStream(new File("exception.txt"), true);
PrintStream ps = new PrintStream(fos);
e.printstacktrace(ps);
答案 2 :(得分:7)
这是一个演示我认为你需要的程序:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
public class StrackTraceAppender {
public static void main(String[] args) {
try {
thrower("Oh noes!");
} catch (Exception e) {
appendToFile(e);
}
try {
thrower("I died!");
} catch (Exception e) {
appendToFile(e);
}
}
public static void thrower(String message) throws Exception {
throw new RuntimeException(message);
}
public static void appendToFile(Exception e) {
try {
FileWriter fstream = new FileWriter("exception.txt", true);
BufferedWriter out = new BufferedWriter(fstream);
PrintWriter pWriter = new PrintWriter(out, true);
e.printStackTrace(pWriter);
}
catch (Exception ie) {
throw new RuntimeException("Could not write Exception to file", ie);
}
}
}
它使用Throwable上的printStackTrace(PrintWriter)
方法将整个堆栈跟踪打印到名为“exception.txt”的文件的末尾,然后有一个main()
方法,用于演示两个示例异常的用法。如果你在IDE中运行它,你会发现你得到一个写有两个堆栈跟踪的文件(适用于我)。
答案 3 :(得分:5)
使用
FileWriter fstream=new FileWriter("exception.txt", true);
创建一个附加文件编写器。
答案 4 :(得分:2)
试试这个:
function lookUpProfile (firstName, prop) {
// Only change code below this line
for (var i = 0; i <= contacts.length; i++) {
if (contacts[i].firstName === firstName) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else {
return "No such property";
}
}
}
//At this point you know for sure that you haven't
//found the contact you're looking for.
return "No such contact";
}
答案 5 :(得分:1)
试试这个:
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.*;
public class ErrorLogger
{
private Logger logger;
public ErrorLogger()
{
logger = Logger.getAnonymousLogger();
configure();
}
private void configure()
{
try
{
String logsFolder = "logs";
Files.createDirectories(Paths.get(logsFolder));
FileHandler fileHandler = new FileHandler(logsFolder + File.separator + getCurrentTimeString() + ".log");
logger.addHandler(fileHandler);
SimpleFormatter formatter = new SimpleFormatter();
fileHandler.setFormatter(formatter);
} catch (IOException exception)
{
exception.printStackTrace();
}
addCloseHandlersShutdownHook();
}
private void addCloseHandlersShutdownHook()
{
Runtime.getRuntime().addShutdownHook(new Thread(() ->
{
// Close all handlers to get rid of empty .LCK files
for (Handler handler : logger.getHandlers())
{
handler.close();
}
}));
}
private String getCurrentTimeString()
{
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
return dateFormat.format(new Date());
}
public void log(Exception exception)
{
logger.log(Level.SEVERE, "", exception);
}
}
用法:
ErrorLogger errorLogger = new ErrorLogger();
try
{
throw new Exception("I died!");
} catch (Exception exception)
{
errorLogger.log(exception);
}