exception handling, creating log and continue the

2020-05-08 08:20发布

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.

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.

3条回答
兄弟一词,经得起流年.
2楼-- · 2020-05-08 08:51

the case is, in this kind of a question, you should better provide us a sample code, then only we can identify the problem without any issue.

If you just need to view the error, then "e.printStackTrace" will help you. The "e" is an instance of class "Exception".

However, if you need to LOG, then "Logger" class will help you, with Exception class.For an example,

try {
                    f = location.createNewFile();
                } catch (IOException ex) {
                    Logger.getLogger(TestForm.class.getName()).log(Level.SEVERE, null, ex);
                }

To do all of these, it is better to surround your code with try catch block

查看更多
我只想做你的唯一
3楼-- · 2020-05-08 09:09
OutputStream os = ....;
PrintStream  ps = new PrintStream(os);

while(notDone) {
    try {
        doStuff();
    }
    catch(Throwable t) {
        t.printStackTrace(ps);
    }
    ps.print(results);
}
查看更多
神经病院院长
4楼-- · 2020-05-08 09:10

You're looking for the try-catch block. See, for example, this tutorial.

查看更多
登录 后发表回答