You can define your own object and add an error code field, but this really
ought not to be used for most scenarios. The preferred way in C# is to throw
and catch specific exception types and take actions based on the exception
type, not on a numeric value within the object. It's not that it cannot be
done, but in most cases it should not be done.
If you are approaching this from the perspective of dealing with exceptions
thrown by other code then you should use the exception type to determine how
to handle the error. From the perspective of writing code that throws
exceptions, you should throw an exception that most specifically indicates
the nature of the problem. If you expect a caller to be able to take
programmatic action based on the exception type, and none of the predefined
types is suitable, then you can define your own exception type and throw
that. If there's no programmatic action that can be taken then you can throw
one of the base exception types.
Dave