Reputation: 702
I am in situation where I need to find the best possible approach to do this,
try
{
// Service call.
}
catch (FaultException exception)
{
service.Abort();
throw new FaultException(Resources.UnexpectedErrorOccurredAtServer, exception);
}
Or
catch (FaultException exception)
{
service.Abort();
throw new Exception(Resources.UnexpectedErrorOccurredAtServer, exception);
}
// Caller.
Main()
{
try
{
serviceCaller()
}
catch(FaultException ex)
{
// Should we have this catch???
}
catch( Exception ex)
{
// Handle unexpected errors.
}
what will be the best approach to throw the expected exception to caller. If we throw FaultException caller main method should Handle it explicitly or general exception will work.
Upvotes: 0
Views: 3540
Reputation: 172378
You may be interested to check WCF error handling and some best practices
The best approach would be like this:-
try
{
proxy.SomeOperation();
}
catch (FaultException<MyFaultInfo> ex)
{
// only if a fault contract was specified
}
catch (FaultException ex)
{
// any other faults
}
catch (CommunicationException ex)
{
// any communication errors?
}
Upvotes: 1
Reputation: 28097
The best approach would be
try
{
// ...
}
catch (FaultException ex)
{
service.Abort();
throw;
}
This preserves the stack trace, which your proposed methods do not. See Throwing Exceptions best practices.
Upvotes: 4