.net - C++/CLI good coding practice, Indexed properties if-check or try..catch? -
a rather short , simple question.
let's use piece of code:
public ref class foo { private: system::collections::generic::dictionary<system::string ^,system::string ^> ^ adictionary; public: property system::string ^ someindexedproperty[system::string ^] { public: system::string ^ get(system::string ^ index) { return adictionary[index]; } } public: foo(void) { adictionary = gcnew system::collections::generic::dictionary<system::string ^,system::string ^>(); } };
would better surround/pre-check return if statement ( if( adictionary->containskey(index) )
or better surround return statement try..catch block?
in both cases returning nullptr when fail.
speed not of issue. general "this better reason" suffice.
i firmly believe if there legal condition reasonably allow, should not catch exception detect it. in other words, use if
statement. akin letting loops on arrays run out until arrayindexofboundsexception
, try...catch oblivion.
on related note, might make sense property throw keynotfoundexception
instead o returning null
. callers might take null
, try dereference it, shifts focus , makes harder find bug.
Comments
Post a Comment