Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Is it good practice to use TRY-CATCH in this way?

Is it good practice to use TRY-CATCH in this way?

Scheduled Pinned Locked Moved C / C++ / MFC
comquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    paresh_joe
    wrote on last edited by
    #1

    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

    H B 2 Replies Last reply
    0
    • P paresh_joe

      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

      H Offline
      H Offline
      Hector_M
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • P paresh_joe

        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

        B Offline
        B Offline
        bulg
        wrote on last edited by
        #3

        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.[^]

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups