Exception handling in Sitecore

Exception handling in Sitecore

There are two use cases for exceptions and logging handler in Sitecore which I am quickly clarify the differences.

1- Sitecore.Diagnostics.Assert
2- Sitecore.Diagnostics.Log

In some scenarios your code throw an exception which should be handles in try/catch blocks. However, in others you would check if the condition is not met then you are going to raise and throw the exception as well as logging the error message.

 Sitecore.Diagnostics.Assert.IsNotNull((object)parameter, "No item in parameters");   

This is going to throw and log the error.

   try
                {
                   //Sending Email
                }
                catch (Exception e)
                {
                Sitecore.Diagnostics.Log.Error(Constants.Messages.ExceptionMessage,e, "SendMail");
                 }

In the second scenario, the error actually happend and you are going to catch it and log it.