when implementing / using methods that return or work with instances of objects, what is the most elegant approach for checking function parameters?
Call Method:
someType GetSomething(object x) { if (x == null) { return; }
or better:
someType GetSomething(object x) { if (x == null) { throw new ArgumentNullException("x"); }
Call Method:
void SomeOtherMethod() { someType myType = GetSomething(someObject); if (someType == null) { return; } }
or better:
void SomeOtherMethod() { try { someType myType = GetSomething(someObject); } catch (ArgumentNullException) { } }
When looking at similar questions, the reason not to use try / catch is performance. But IMHO try-catch just looks better :).
So which way is more "graceful"?
methods c # exception-handling robustness
Inno
source share