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. assert VS exceptions [modified]

assert VS exceptions [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
visual-studiodebugginghelpquestionannouncement
5 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.
  • B Offline
    B Offline
    big_denny_200
    wrote on last edited by
    #1

    hi all :) I saw this code :

    inline int foo (char* pBuffer, const char* formatString) throw() {

    assert (pBuffer);
    assert (formatString);

    return "something";
    }

    If i use this code and in Release build and pass pBuffer, whose value will be NULL, than I get a bug yes ? these asserts are useful only in debug mode, not ? Is not it better to above this function like this :

    inline int foo (char* buffer, const char* formatString) {

    if(NULL == buffer)
    throw someException();

    if(NULL == formatString);
    throw someOtherException();

    return "something";
    }

    In this case if we catch the thrown exception than, we are secure in both Release ana Debug builds, not ? so why use asserts and not throws? thank you -- modified at 4:01 Monday 29th May, 2006

    _ S 2 Replies Last reply
    0
    • B big_denny_200

      hi all :) I saw this code :

      inline int foo (char* pBuffer, const char* formatString) throw() {

      assert (pBuffer);
      assert (formatString);

      return "something";
      }

      If i use this code and in Release build and pass pBuffer, whose value will be NULL, than I get a bug yes ? these asserts are useful only in debug mode, not ? Is not it better to above this function like this :

      inline int foo (char* buffer, const char* formatString) {

      if(NULL == buffer)
      throw someException();

      if(NULL == formatString);
      throw someOtherException();

      return "something";
      }

      In this case if we catch the thrown exception than, we are secure in both Release ana Debug builds, not ? so why use asserts and not throws? thank you -- modified at 4:01 Monday 29th May, 2006

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      Programming errors are not exceptions. You can't remove bugs until and unless you fix them. Once you have found and fixed all of the bugs,the assert macros are no longer necessary and you should turn them off by defining the NDEBUG macro. Assertions show that you are still testing and debugging your code. An exception is an unpredictable but expected event which cannot be prevented and must be handled. Exceptions should be handled to the extent possible at the point where they are first detected. Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

      B 1 Reply Last reply
      0
      • _ _AnsHUMAN_

        Programming errors are not exceptions. You can't remove bugs until and unless you fix them. Once you have found and fixed all of the bugs,the assert macros are no longer necessary and you should turn them off by defining the NDEBUG macro. Assertions show that you are still testing and debugging your code. An exception is an unpredictable but expected event which cannot be prevented and must be handled. Exceptions should be handled to the extent possible at the point where they are first detected. Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

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

        _AnShUmAn_ wrote:

        Once you have found and fixed all of the bugs

        How can you know that you have eliminated all bugs ?

        _AnShUmAn_ wrote:

        Assertions show that you are still testing and debugging your code

        I see asserts in already posted codes...

        _AnShUmAn_ wrote:

        An exception is an unpredictable but expected event

        :confused:, why unpredictable. Can not understang it fully :( anyway thanks for reply

        _ 1 Reply Last reply
        0
        • B big_denny_200

          hi all :) I saw this code :

          inline int foo (char* pBuffer, const char* formatString) throw() {

          assert (pBuffer);
          assert (formatString);

          return "something";
          }

          If i use this code and in Release build and pass pBuffer, whose value will be NULL, than I get a bug yes ? these asserts are useful only in debug mode, not ? Is not it better to above this function like this :

          inline int foo (char* buffer, const char* formatString) {

          if(NULL == buffer)
          throw someException();

          if(NULL == formatString);
          throw someOtherException();

          return "something";
          }

          In this case if we catch the thrown exception than, we are secure in both Release ana Debug builds, not ? so why use asserts and not throws? thank you -- modified at 4:01 Monday 29th May, 2006

          S Offline
          S Offline
          Sarath C
          wrote on last edited by
          #4

          asserts are using only for debug pupose. suppose if ur buffer getting NULL a endless loop or a lengthy looop, u need not to debug the each loops. if an assertion occrurs then it will show "This expression failed at this line of source code" then u can trace and correct the bug. never put ur logic inside the assert macro. because it wont work in release build. in release build VERIFY macro will satisfy u with the same functionality of assert offer. I think asserts and exception standing at different areas of erro handling. We are used to compare with strctured error handling and the normal error handling we are used to follow in "C". it is depends on ur system to select which is suitable. each mechanism has its own advantages and disadvantages. SaRath

          1 Reply Last reply
          0
          • B big_denny_200

            _AnShUmAn_ wrote:

            Once you have found and fixed all of the bugs

            How can you know that you have eliminated all bugs ?

            _AnShUmAn_ wrote:

            Assertions show that you are still testing and debugging your code

            I see asserts in already posted codes...

            _AnShUmAn_ wrote:

            An exception is an unpredictable but expected event

            :confused:, why unpredictable. Can not understang it fully :( anyway thanks for reply

            _ Offline
            _ Offline
            _AnsHUMAN_
            wrote on last edited by
            #5

            if the application is performing as per your expectations with no errors or CRASHES you should know that all the bugs are fixed. It's that simple. So when you are done with this disable all ASSERTS by defining nDEBUG macro. Exceptions are unpredictable because in some applications if many threads are running simultaneously than there may be a variable that would get modified in the other sections and cause an exception in another block but this is not guaranteed . It depends on the time the system allocates to each thread. Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

            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