check with If or just use try

M

mp

say i want to get an object from another objects method return
List<string> keylist = xlrdr.RangeValueList();

if keylist could be null...would i
if(keylist != null)
{
foreach (string str in keylist)
{ Debug.Print(str); }
}
else
{...handle null condition}

or should one just try

try
{ foreach (string str in keylist)
{ Debug.Print(str); }
}
catch
{...handle exception}


or should RangeValueList have raised an exception instead of returning null?
so the calling code doesn't have to mess with it
?
thanks
mark
 
A

Arne Vajhøj

say i want to get an object from another objects method return
List<string> keylist = xlrdr.RangeValueList();

if keylist could be null...would i
if(keylist != null)
{
foreach (string str in keylist)
{ Debug.Print(str); }
}
else
{...handle null condition}

or should one just try

try
{ foreach (string str in keylist)
{ Debug.Print(str); }
}
catch
{...handle exception}


or should RangeValueList have raised an exception instead of returning null?
so the calling code doesn't have to mess with it
?

You should not use try catch to handle null pointers, so with that
API then you should test for null.

My preferences would probably be either throw an exception or
returning a List with no elements depending on what makes sense
in the specific context.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top