Throw exception vs Logging

Normally, I’d argue that you should either log or rethrow. Doing both will just cause every layer to log the exception again and again, which makes the logs hard to read. Even worse, it’s hard to figure out how many errors you actually have – was it seven errors, or seven layers of the app which logged the same error?

This means that if you suppress an exception, you log it and say why you didn’t think it was worth rethrowing.

On the other hand, if you re-throw the exception, you know it’s either going to be caught and suppressed (in which case the catcher logs the exception and why it was suppressed), or it will bubble up out of your app and be caught by the app container, which will catch and log the exception. Every exception shows up once and only once in the logs.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)