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);