Friday, November 12, 2010

Throw exception

When using throw after catching and handling an exception:


private void SaveRecord(Record record)
{
try
{
var db = new NorthwindDB();
db.SaveRecordInfo(null, record.ClientID, DateTime.Now).Execute(); // let's say this is the line 45
}
catch (Exception ex)
{
_logger.Error("Failed saving db record", ex);
throw;
}
}


We will get something like the following:


System.Transactions.TransactionManagerCommunicationException: blah blah blah ...
at SampleApp.DataAccess.Repository.SaveRecord(Record record) in C:\Projects\SampleApp.DataAccess\Repository.cs:line 45


Which is what we want, in this example we want to log the exception and then re-throw it without losing the stack trace info (and the exact line of code where the exception occurred)

No comments: