I see the need to copy and paste the following error handling code several times. What are my options when working within a Catch statement?
-
In doing so, will I lose valuable information in the process? (example: are exceptions re-wrapped in another exception, or loss of stack information)
-
How can someone tell the difference between a “throw” in myAbstractClass and one in the Select method below?
Here is sample code I want to replicate
public class StackUserDataSource : AbstractEnhancedTableDataSource<StackUserDataServiceContext>
{
//.. stuff
public IEnumerable<StackUserDataModel> Select()
{
try
{
var results = from c in _ServiceContext.StackUserTable
select c;
var query = results.AsTableServiceQuery();
var queryResults = query.Execute();
return queryResults;
}
catch (StorageClientException e)
{
// Todo: consider sticking this in another central location
switch (e.ErrorCode)
{
case StorageErrorCode.AccessDenied:
break;
case StorageErrorCode.AccountNotFound:
break;
case StorageErrorCode.AuthenticationFailure:
break;
// ... Yadda yadda, handle some exceptions, not others.. this is a demo.
case StorageErrorCode.TransportError:
break;
default:
break;
}
throw;
}
}
Update:
I doubt this is possible, but can I dynamically catch and filter exceptions in an external library? The concept goes like this
try
{
var results = from c in _ServiceContext.StackUserTable
select c;
var query = results.AsTableServiceQuery();
var queryResults = query.Execute();
return queryResults;
}
catch (MyExternalExceptionHelperDLL e)
{
// all exceptions referenced in MyExternalHelper are passed below
MyExternalExceptionHelper.ProcessException(e);
}
catch (exception)
{
}
Since it’s probably not possible for the MyExternalExceptionHelperDLL to dynamically pick and choose what to listen for (i.e. SQL, vs Networking vs File, but not authentication)
try
{
var results = from c in _ServiceContext.StackUserTable
select c;
var query = results.AsTableServiceQuery();
var queryResults = query.Execute();
return queryResults;
}
catch (exception e)
{
MyExternalExceptionHelper.ProcessException(e);
// The problem is that I don't know how to catch exceptions thrown from that static method above,
// or how to override that exception handling...
}
But with the above code, its not clear to me how the end user can opt into or override my methods of event handling.
You could do something like this. The important part is that the
throwneeds to be in the originalcatchblock in order to preserve the stack trace.