language agnostic - Preferred way of checking for errors -
if want say, check int
between 2 numbers, better implement without exceptions so:
int a; //example in c++, translate language of choice... cin >> a; if(a < 0 || > 5) cout << "you must enter in number between 0 , 5 inclusive!" << endl;
or exception in try-catch block?
try { int a; cin >> a; if(a < 0 || > 5) throw "you must enter in number between 0 , 5 inclusive!" << endl; } catch(string msg) { cout << msg << endl; }
neither 'better'. how deal anomalous conditions, errors, etc. totally depends on exact circumstances , design of code. try-catch blocks great when error should cause following code skipped. on other hand, when want let user know problem continue anyways by, say, assuming default value, try-catch may not appropriate.
additionally, programs designed custom error-handling code errors , anomalies can logged , recorded, , ensure program can fail gracefully. if working kind of system, may not using 'throw' @ all, instead errorhandler.logerror("something went wrong!");
Comments
Post a Comment