What to include in the catch clause exceptions

I have code that throws a bunch of exceptions, but each of them contains only the printStackTrace () method, as shown below.

} catch (SecurityException e) {
    // TODO Auto-generated catch block
    System.err.println(e);
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Is this sufficient or do I need to include additional instructions such as System.err.println (e)? Usually, if an exception occurs, I can only track the source using the above.

+3
source share
5 answers

If you can do something to solve the problem, do it in catch, if you can do nothing, then it is better to use the logging framework to register an exception than to use e.printStackTrace();orSystem.err.println(e);

: http://www.slf4j.org/, , ( ) Java Logging API: http://download.oracle.com/javase/1.4.2/docs/guide/util/logging/.

SLF4J , Java Logging API ( API - (de jure not de facto) ""

SLF4J , , , logger.error("some accompanying message", exception);, , , , , ( logback )

+8

. , printStackTrace() ( ). .

: IOException, , , , . , ... ..

+1

e.printStackTrace(), . e.printStackTrace(); DEBUG. ERROR .

+1

, .

, .

, , , - , .

, , , . , , . , , . , , , .

, . , ( ).

+1

If you can afford to use a registration framework like log4j , you can call

}catch(Exception e){ log.error("Exception occurred:",e}

creating a log structure to record your custom “Exception Occurred” message, followed by a stack trace in the errorlog file

0
source

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


All Articles