Is it good practice to use TRY-CATCH in this way?
-
I am writing block which throws com exception and i have written code in following way. GetDate() & SetDate(d) both throws exception. Date d; try { // Many statements can throws exception d = GetDate(); SetDate(somedate); // calculate some "value" } catch(_com_exception &) { // set value = 0; } try { SetDate(d); // reset date } catch(_com_exception &) { // set value = 0; } Even if both block throws same exception i cant put them in one try for the reason that in any case either it throws exception or not i want to reset date at the end. Is this code looks good with consideration of good coding practices? Thanks, Perry
-
I am writing block which throws com exception and i have written code in following way. GetDate() & SetDate(d) both throws exception. Date d; try { // Many statements can throws exception d = GetDate(); SetDate(somedate); // calculate some "value" } catch(_com_exception &) { // set value = 0; } try { SetDate(d); // reset date } catch(_com_exception &) { // set value = 0; } Even if both block throws same exception i cant put them in one try for the reason that in any case either it throws exception or not i want to reset date at the end. Is this code looks good with consideration of good coding practices? Thanks, Perry
Well, in my opinion it depends on the goal of your app, since at least for me,it is good to make fully diference between every error code, and in your app. it seems that the situations are different, so for me this is good since personally, i hate long "if"/"try-catch" statements. regards, Hector.
-
I am writing block which throws com exception and i have written code in following way. GetDate() & SetDate(d) both throws exception. Date d; try { // Many statements can throws exception d = GetDate(); SetDate(somedate); // calculate some "value" } catch(_com_exception &) { // set value = 0; } try { SetDate(d); // reset date } catch(_com_exception &) { // set value = 0; } Even if both block throws same exception i cant put them in one try for the reason that in any case either it throws exception or not i want to reset date at the end. Is this code looks good with consideration of good coding practices? Thanks, Perry
I may be wrong, but Exceptions seem to me to be an unexpected error (out of bounds memory, couldn't allocate enough memory, divide by zero), while something like HRESULT return value schemes tend to look for and handle expected errors. So to answer whether or not using try{}catch{} is good practice, you have to explain (to yourself or to us) why you're using them. Sorry for the generic answer, but coding is all about using the right tool for the job, just like anything else - otherwise you end up in this forum too often.[^]