0

I have a variable which I know will be invalid if it is zero, so would like to catch this situation. I started coding this as an ArgumentOutOfRangeException but when I do this I get a blue squiggly line appearing under the first parameter for this exception type (paramName); as the variable I am referencing is not an argument of the procedure.

The value is being set from a call to a separate assembly (possibly not written by 'us', so am looking to handle the exception locally)

I've spent a while Googling, and looking at the options in intellisense, but can't see predefined exception that fits. Any suggestions for a better option than leaving as is or using the generic Exception?

Example of what I am doing for clarity:

    public long MyProcedure(long incomingVariable)
    {
        long eventId = ThirdParty.GetEventId(incomingVariable);

        if (eventId == 0)
        {
            eventId = ThirdParty.SecondaryCall();
        }

        if (eventId == 0)
        {
            throw new ArgumentOutOfRangeException("eventId", "Event ID of zero");
        }
     }
d219
  • 2,707
  • 5
  • 31
  • 36
  • 2
    Have you considered creating your own `Exception` class? – mjwills Feb 20 '18 at 11:02
  • Actually no, didn't realise you could. Will have a look at that option. – d219 Feb 20 '18 at 11:04
  • Possible duplicate of [creating my own exceptions c#](https://stackoverflow.com/questions/20391287/creating-my-own-exceptions-c-sharp) – mjwills Feb 20 '18 at 11:05
  • 1
    It looks like`GetEventId` should be the one to throw an exception if it can't return a valid id. – Lee Feb 20 '18 at 11:05
  • @Lee - I should amend there actually, I simplified the code but GetEventId is going to a separate assembly that may be written by a third party. Will make an edit – d219 Feb 20 '18 at 11:08
  • 2
    You can do `throw new ArgumentException("No event found for incomingVariable " + incomingVariable, nameof(incomingVariable))`, because `incomingVariable` is what is invalid, not `eventId`. – Evk Feb 20 '18 at 11:12
  • Will `ThirdPart.GetEventId` only return 0 if the value of `incomingVariable` is invalid? – Lee Feb 20 '18 at 11:17
  • @lee- sorry the incomingVariable may be a bit of a red herring - I simplified the code but it actually makes a secondary call (will edit to show what I mean), but no incomingVariable can be valid and zero still returned. – d219 Feb 20 '18 at 11:20

1 Answers1

0

InvalidOperationException is the generic, catch all, "You've called a valid method, there's no specific issue with any particular parameters, but I'm not now in the correct state to actually function" exception.

InvalidOperationException is used in cases when the failure to invoke a method is caused by reasons other than invalid arguments. Typically, it is thrown when the state of an object cannot support the method call.

Some framework classes further derive a more specific exception from InvalidOperationException, which you may want to consider doing if there's additional "structured" data you think would be useful for consumers. Also, worth paying attention to this:

Because the InvalidOperationException exception can be thrown in a wide variety of circumstances, it is important to read the exception message returned by the Message property.

Which is written from the consumers perspective - but I'd suggest that it should also be interpreted to say "make sure that any exception message you provide is specific and contains enough detail".

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448