我应该在日志中还是在异常中或同时在两者中打印错误消息?

时间:2020-02-07 15:50:14

标签: error-handling error-logging

我在Scala中有以下代码:

logger.error("Repository does not exists or does not contains 'definitions' directory")
throw new FileNotFoundException("Repository does not exists or does not contains 'definitions' directory")

我应该写两次消息吗?还是我应该只记录错误并抛出空异常?

1 个答案:

答案 0 :(得分:1)

取决于用例,要视用例而定。 如果您的日志语句在dev-scope内,则无需向源/调用方抛出异常。您可以记录异常并将其静音,然后将有用的消息/代码返回给源/调用者。