Reputation: 40002
I have to process items off a queue.
Deleting items off the queue is a manual call to Queue.DeleteMessage
. This needs to occurs regardless of whether or not the processing succeeds.
var queueMessage = Queue.GetMessage();
try
{
pipeline.Process(queueMessage);
}
catch (Exception ex)
{
try
{
Logger.LogException(ex);
}
catch { }
}
finally
{
Queue.DeleteMessage(queueMessage);
}
Problem:
On failure, I log the error to some data store. If this logging fails (perhaps the data store is not available), I still need the message to be deleted from the queue.
I have wrapped the LogException
call in another try catch
. Is this the correct way or performing thing?
Upvotes: 1
Views: 329
Reputation: 73442
Following code is enough. finally
blocks execute even when exception is thrown in catch
block.
var queueMessage = Queue.GetMessage();
try
{
pipeline.Process(queueMessage);
}
catch (Exception ex)
{
Logger.LogException(ex);
}
finally
{
Queue.DeleteMessage(queueMessage);//Will be executed for sure*
}
Upvotes: 2
Reputation: 514
The finally block always executes, even if it throws an unhandled error (unless it end the app). So yes.
Upvotes: 1