tags:

views:

44

answers:

2

ArgumentException and ArgumentNullException are both used for validating arguments, eg.

if (argument == null)
    throw new ArgumentNullException("argument must not be null", "argument");

What is the best equivalent for validating the results of callbacks, eg.

var x = argument.GetX();
if (x == null)
    throw ???
A: 

Hi there.

I personally use ArgumentException or ArgumentNullException in conjunction with testing parameters passed to routines, like this:

Public Sub Test(s As Object)

If s Is Nothing Then
  Throw New ArgumentNullException("s")
End If

End Sub

In your case, I would just throw a normal Exception, since the variable 'x' is not an argument passed to your routine, it's a local variable. I would recommend using ArgumentException or ArgumentNullException for parameters passed to routines rather then local variables.

Cheers. Jas.

Jason Evans
+1  A: 

Without knowing much about your code, I can suggest throwing InvalidOperationException.

Alternatively, if this piece of code is important, you can create custom exception, say XLoadException, and throw that.

SolutionYogi
In my case an XmlException made the best sense, but it still seems like there should be a better solution here.
Oliver Hallam