c# - What is better: int.TryParse or try { int.Parse() } catch -


i know.. know... performance not main concern here, curiosity, better?

bool parsed = int.tryparse(string, out num); if (parsed) ... 

or

try {     int.parse(string); } catch () {     something... } 

better highly subjective. instance, prefer int.tryparse, since don't care why parsing fails, if fails. however, int.parse can (according documentation) throw 3 different exceptions:

  • the input null
  • the input not in valid format
  • the input contains number procudes overflow

if care why fails, int.parse better choice.

as always, context king.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -