Cannot catch FileNotFoundException when using DOMConfigurator

I have the following code:

try {  
    DOMConfigurator.configure(url+log4j.xml);
} catch(Exception e) {   
    e.printStackTrace();   
}

And I would expect FileNotFoundExceptionif log4j.xml does not exist and then the catch block will be executed.

But I do not see an exception when the file does not exist, why?

+3
source share
3 answers

If you look at the source of DOMConfigurator.doConfigure , it looks like it catches Exception, and then just logs the error and not solves it, so FileNotFoundExceptionit won’t return to your calling code.

try {
...
} catch (Exception e) {
    if (e instanceof InterruptedException || e instanceof InterruptedIOException) {
        Thread.currentThread().interrupt();
    }
    // I know this is miserable...
    LogLog.error("Could not parse "+ action.toString() + ".", e);
} 

To get around this, you can pre-check if the file exists.

+2
source

Throwable Exception . , .

0

If you want to disable these messages from log4j, you can install log4j in silent mode:

LogLog.setQuietMode(true);
0
source

Source: https://habr.com/ru/post/1770286/


All Articles