异常处理,创建日志并在JAVA中继续程序 [英] exception handling, creating log and continue the program in JAVA

查看:95
本文介绍了异常处理,创建日志并在JAVA中继续程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在JAVA中设计一个程序,可以在大约10次迭代中捕获结果。在这些迭代结束时,必须将所有结果写入日志文件。

I am designing a program in JAVA that captures results in about 10 iterations. At the end of these iterations all the results must be written into a log file.

如果发生任何异常,那么它应该写在我的文本文件上,其次程序不能停止,它必须继续,直到最后一次迭代完成......

If any exception occurs then it should be written on my text file and secondly the program must not stop, it must go on till the last iteration is completed...

也就是说 - 如果在任何迭代的任何部分发生某些错误,程序就不能在这里停止。必须在我的结果中通过错误名称提及错误,并且必须继续并更新我的日志文件。

That is to say - if some error occur on any part of any iteration the program must not stop here. The error must be mentioned within my results by the name of error and it must go on and update my log file.

我的代码到目前为止有点冗长...使用try-catch,try块正在进行我的计算和编写我的文本文件,但我需要如果发生一些异常,我的程序必须不能停止,并且必须在我的日志文件中更新该异常。

My code till now is bit lengthy...used try-catch, the try block is doing my calculations and writing my text file, but I need if incase some exception occurs my program must not stop and that exception must be updated in my log file.

推荐答案

OutputStream os = ....;
PrintStream  ps = new PrintStream(os);

while(notDone) {
    try {
        doStuff();
    }
    catch(Throwable t) {
        t.printStackTrace(ps);
    }
    ps.print(results);
}

这篇关于异常处理,创建日志并在JAVA中继续程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆