7

I have a requirement to throw an exception if I encounter a specific error and return an object in that case.

So I want to do something like this:

if(error)
return obj;
throw new FaultException(string.Format("Error found with ID={0}", ID))

This will not work obviously, as return will prevent the exception from being thrown.

Can anyone suggest whats the best way to achive this. Can I pass the object in the exception somehow?

Thanks

gunnerz
  • 1,898
  • 5
  • 24
  • 39

5 Answers5

9

Normally you would create your own exception class which would contain all your data. But I see FaultException which implies WCF, and for that there's already FaultException<T>.

Sergei Rogovtcev
  • 5,804
  • 2
  • 22
  • 35
6

In the context of some method, throwing exception and returning result are mutually exclusive things.

So, if you want to provide the client code with the additional information, you can implement custom exception (derive from Exception class) with custom properties.

[Serializable]
public class MyException : Exception
{
    public MyCustomObject CustomObject { get; private set; }

    public MyException(MyCustomObject customObject)
    {
        CustomObject = customObject;
    }

    public MyException(string message, MyCustomObject customObject)
        : base(message)
    {
        CustomObject = customObject;
    }

    public MyException(string message, Exception inner, MyCustomObject customObject)
        : base(message, inner)
    {
        CustomObject = customObject;
    }

    protected MyException(
        SerializationInfo info,
        StreamingContext context)
        : base(info, context)
    {
        // TODO: Implement serializable stuff.
    }

    #region Overrides of Exception

    public override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        // TODO: Implement serializable stuff.
        base.GetObjectData(info, context);
    }

    #endregion
}

Now you can throw it!

throw new MyException(obj);
4

You are correct that you cannot both return an object and throw an exception. If that is your requirement, I would suggest trying to revise it.

But I would say you're on the right track, come up with an exception that includes the return-object. Catch the exception and get the "returned" value from it.

Edit: I found a very interesting discussion a while back regarding throwing exception vs returning error code. Check it out. I still think you're better of going with one or the other.

Community
  • 1
  • 1
Mizipzor
  • 51,151
  • 22
  • 97
  • 138
0

If you want to get context from the extension, put it inside Exception.Data or create your own exception class and add a member to it (this will work better if the exception needs to be serialized).

It would probably be preferable however (but can't tell without context) if you refactored the code and/or reconsidered the requirements so that you didn't need to do this at all.

Jon
  • 428,835
  • 81
  • 738
  • 806
0

You could add an exception property to whatever type obj is and return obj with an associated Exception (if one is thrown) and then check the presence of the Exception in the calling code for this method, or wherever you are returning obj to.

Slightly messy but may suit the setup you've described.

SpaceBison
  • 3,704
  • 1
  • 30
  • 44