• Post Reply Bookmark Topic Watch Topic
  • New Topic

log4j vs. exceptions  RSS feed

 
John Landon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Really stupid question but...
I am using lig4j. So I have log.error("blbla"). Why do I need to throw exceptions then.

thanks.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exceptions are thrown to allow a program to handle an error condition. Logging an error is for a person (a developer or operations support person) to have a record of the fact that something went wrong and for use in troubleshooting and debugging. You may also need to log it for auditing and or legal reasons in some cases. (Often you not only log errors, but other information like a successful application startup or shutdown, when a user logs in, when a file is being rolled, etc.)

Logs also act as good historical records. Let's say I have a network connection exception. Let's say my error handling for that is to wait 30 seconds and then try again. So the code logs the first connection failure, then throws an exception. The code that handles that exception tries again. If it connects ok, it moves on. If it fails a second time, it then exits the program. Without logging, I would never know the connection exception occurred. But with logging, not only do I know it occurred, but how frequently. I am far more concerned with the fact that I am having 50 network connection failures a day than if I am having 1 every 6 months. Without logging I won't know this. (of course, this implies that I am periodically checking my logs for problems.)

Logging can also provide context. If I look at my logs and see that I have network connection issues every day between 2:00am and 3:00am, the same time my backup is running, I better know how to troubleshoot the issue.

So exceptions and (error) logging serve different purposes.
 
Bert Bates
author
Sheriff
Posts: 8954
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
off to the intermediate forum
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!