I have my own wcf service class which should as result return string with “Everything Save Saccesfully” if ok save data. Otherwise I need to return in this string exception.
string SaveData(Stream stream)
{
string error = "";
try
{
//do saving data
error+="Everything Save Saccefully";
return error;
}
catch(Exception ex)
{
throw ex;
}
finally
{
}
}
It is possible to catch errors occurs in try block and return it in error variable ?
Normally I would do as suggested by Jon, but if you really don’t want to allow any exceptions to bubble up from your service you could encapsulate your success and (any) failure error information in a class structure like this
And then…
The downside of this approach is that your caller must check the
Successvalue after callingSaveData. Failure to do so can result in a bug in your code that will only manifest itself if the save fails.Alternatively, if you don’t handle the exception you get to benefit from one of the useful things about structured exception handling: If the caller forgets to explicitly handle any exception then it will bubble up the call stack and either crash the app or get caught by some higher-level error handling code.
Not necessarily ideal, but generally better than your code silently assuming that something succeeded, when in fact it didn’t.