exception handling, creating log and continue the program in JAVA

前端 未结 3 1215
暖寄归人
暖寄归人 2021-01-28 13:04

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 ex

相关标签:
3条回答
  • 2021-01-28 13:26
    OutputStream os = ....;
    PrintStream  ps = new PrintStream(os);
    
    while(notDone) {
        try {
            doStuff();
        }
        catch(Throwable t) {
            t.printStackTrace(ps);
        }
        ps.print(results);
    }
    
    0 讨论(0)
  • 2021-01-28 13:38

    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

    0 讨论(0)
  • 2021-01-28 13:43

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

    0 讨论(0)
提交回复
热议问题