UPDATE:
This gives more information about what you want to do. Since you cannot throw exceptions, you must have a base class of results. I usually do this for WCF methods, which I call through javascript, since it cannot handle exceptions nicely.
So you need a base class, for example:
[DataContract] public class AjaxResult { public static AjaxResult GetSuccessResult() { return new AjaxResult(); } [DataMember] public int Status { get; set; } [DataMember] public string Error { get; set; } }
You can then inherit this by adding any data you want to return. This example returns a single product object and a list of validation errors.
[DataContract] public class SingleProductResult : AjaxResult { [DataMember] public Product Data { get; set; } [DataMember] public IList<int> ValidationErrors { get; set; } }
You can also create a universal wrapper so you don't have to write a lot of code in your methods. I usually put this in a base class and let all WCF services inherit from this class.
protected T PerformAjaxOperation<T>(Func<T> action) where T : AjaxResult, new() { try { return action(); } catch (AccessDeniedException ade) { // -- user tried to perform an invalid action return new T() { Status = AjaxErrorCodes.AccessDenied, Error = ade.ToString() }; } catch (Exception ex) { return new T() { Error = ex.ToString(), Status = 1 }; } }
Then just use it like this:
public SingleProductResult GetProduct(int productId) { return PerformAjaxOperation(() => { return retval = new SingleProductResult() { Data = ProductServiceInstance.GetProduct(productId) }; }); } public AjaxResult DeleteProduct(int productId) { return PerformAjaxOperation(() => { ProductServiceInstance.DeleteProduct(productId); return AjaxResult.GetSuccessResult(); }); }
So, if everything went smoothly, the error will be 0, and the message will be zero. If an exception is thrown, it will be caught by the PerformAjaxOperation() function and populated inside the AjaxResult object (or its derivative) and return to the client.
Previous answer:
I do not think it's a good idea. You can create a custom exception by creating a class that inherits from Exception and add the properties you want to keep there. Then, when an exception occurs, you simply catch it and fill it inside this new exception along with other details. Then throw this exception. Then you can catch this exception at higher levels and display the correct message.
example:
public IList<Articles> GetArticles() { try { return GetSomeArticlesFromDatabase(); } catch (Exception innerException) { throw new MyCustomException("some data", 500, innerException); } } public class MyCustomException : Exception { public int HttpCode { get; set; } public MyCustomException(string errorMessage, int httpCode, Exception innerException) : base(errorMessage, innerException) { HttpCode = httpCode; } } public void EntryPoint() { try { DoSomething(); var result = GetArticles(); DoSomething(); DisplayResult(result); } catch (MyCustomException ex) { ReturnHttpError(ex.Message, ex.HttpCode); } }