Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging exception details in try finally block

I have a small section of code invoking a webservice client method.

The method returns an array of messages as an out parameter. Often these messages contain details of any errors that have occured. When an error does occur an exception is also thrown.

I want to log the messages regardless of whether an exception is thrown or the type of the exception. Is logging in the finally block acceptable practice?

WebServiceClient client = GetWebServiceClient();
Console.WriteLine("Calling getUpdates...");
ItemStatus[] itemStatuses;
Message[] messages = null;
string outToken;
try
{
     outToken = client.getUpdates(inToken, out itemStatuses, out messages);
}
finally
{
     LogMessages(messages);
}
like image 731
TonE Avatar asked Jul 02 '13 08:07

TonE


1 Answers

Yes, a finally block will always be executed (Even if an exception is not thrown). So it kinda makes sense to put the Logging there in this context.

like image 61
Darren Avatar answered Sep 30 '22 17:09

Darren