Hello everyone,
when implementing/using methods that return or work with instances of objects, what is the most elegant approach to check the function parameters ?
Method to call:
someType GetSomething(object x)
{
if (x == null) {
return;
}
//
// Code...
//
}
or better:
someType GetSomething(object x)
{
if (x == null) {
throw new ArgumentNullException("x");
}
//
// Code...
//
}
Calling Method:
void SomeOtherMethod()
{
someType myType = GetSomething(someObject);
if (someType == null) {
return;
}
}
or better:
void SomeOtherMethod()
{
try {
someType myType = GetSomething(someObject);
} catch (ArgumentNullException) {
}
}
When browsing through similar questions, the reason not to use try/catch is performance. But IMHO the try-catch just looks better :).
So, which way is more "elegant"?