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. The Lounge
  3. Exceptions: Catching null memory?

Exceptions: Catching null memory?

Scheduled Pinned Locked Moved The Lounge
performancequestion
7 Posts 6 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.
  • D Offline
    D Offline
    danwatt
    wrote on last edited by
    #1

    Given this: try { MyClass->DoSomething(); } catch (...) { cout << "Unknown exception"; } I am trying to catch a null memory exception (where MyClass is NULL). Is there a specific way to catch this, or am I conformed to using the catch all "..." method, or sticking with "if (MyClass!=NULL)"?

    A F K 3 Replies Last reply
    0
    • D danwatt

      Given this: try { MyClass->DoSomething(); } catch (...) { cout << "Unknown exception"; } I am trying to catch a null memory exception (where MyClass is NULL). Is there a specific way to catch this, or am I conformed to using the catch all "..." method, or sticking with "if (MyClass!=NULL)"?

      A Offline
      A Offline
      Alvaro Mendez
      wrote on last edited by
      #2

      You can use this:

      catch (PostingCPlusPlusQuestionsInLoungeException e)
      {
      cout << "Please use the Visual C++ forum." << endl;
      }

      Regards, Alvaro

      D 1 Reply Last reply
      0
      • D danwatt

        Given this: try { MyClass->DoSomething(); } catch (...) { cout << "Unknown exception"; } I am trying to catch a null memory exception (where MyClass is NULL). Is there a specific way to catch this, or am I conformed to using the catch all "..." method, or sticking with "if (MyClass!=NULL)"?

        F Offline
        F Offline
        Farhan Noor Qureshi
        wrote on last edited by
        #3

        Please post all programming related questions in their respective forums. Please read the heading on the lounge, it states "The Lounge is a place where you can discuss anything that takes your fancy. If you just want to laze about and discuss things that don't quite fit elsewhere, then this is the place." BTW: I use several API functions for e.g. IsBad****(...) to know the status of objects and functions. HTH :) ;) ;P :-D :cool: Farhan Noor Qureshi

        T 1 Reply Last reply
        0
        • A Alvaro Mendez

          You can use this:

          catch (PostingCPlusPlusQuestionsInLoungeException e)
          {
          cout << "Please use the Visual C++ forum." << endl;
          }

          Regards, Alvaro

          D Offline
          D Offline
          danwatt
          wrote on last edited by
          #4

          my bad...

          1 Reply Last reply
          0
          • D danwatt

            Given this: try { MyClass->DoSomething(); } catch (...) { cout << "Unknown exception"; } I am trying to catch a null memory exception (where MyClass is NULL). Is there a specific way to catch this, or am I conformed to using the catch all "..." method, or sticking with "if (MyClass!=NULL)"?

            K Offline
            K Offline
            Konstantin Boukreev
            wrote on last edited by
            #5

            Yes, it's easy. the point in using _set_se_translator API.

            struct se_exception
            {
            unsigned code;
            se_exception (unsigned c) : code(c) {}
            };

            struct se_exception_access_vioalation : se_exception
            {
            se_exception_access_vioalation ()
            : se_exception(EXCEPTION_ACCESS_VIOLATION) {}
            };

            void translator_func(unsigned u, _EXCEPTION_POINTERS *)
            {
            switch (u)
            {
            case EXCEPTION_ACCESS_VIOLATION:
            throw se_exception_access_vioalation();
            break;
            default:
            throw se_exception(u);
            break;
            }
            }

            struct dummy
            {
            char * s;
            };

            int main(int argc, char* argv[])
            {
            _set_se_translator(translator_func);

            dummy \* p = 0;
            
            try
            {		
            	std::cout << p->s <
            

            :)

            J 1 Reply Last reply
            0
            • K Konstantin Boukreev

              Yes, it's easy. the point in using _set_se_translator API.

              struct se_exception
              {
              unsigned code;
              se_exception (unsigned c) : code(c) {}
              };

              struct se_exception_access_vioalation : se_exception
              {
              se_exception_access_vioalation ()
              : se_exception(EXCEPTION_ACCESS_VIOLATION) {}
              };

              void translator_func(unsigned u, _EXCEPTION_POINTERS *)
              {
              switch (u)
              {
              case EXCEPTION_ACCESS_VIOLATION:
              throw se_exception_access_vioalation();
              break;
              default:
              throw se_exception(u);
              break;
              }
              }

              struct dummy
              {
              char * s;
              };

              int main(int argc, char* argv[])
              {
              _set_se_translator(translator_func);

              dummy \* p = 0;
              
              try
              {		
              	std::cout << p->s <
              

              :)

              J Offline
              J Offline
              Jamie Hale
              wrote on last edited by
              #6

              I had never seen that before... might come in handy sometime. Thanks. According to the docs, though, you should save the return value from the _set_se_translator() call and restore it when you are done.

              ...
              _se_translator_function old = _set_se_translator(translator_func);
              ...
              _set_se_translator(old);
              ...

              J

              1 Reply Last reply
              0
              • F Farhan Noor Qureshi

                Please post all programming related questions in their respective forums. Please read the heading on the lounge, it states "The Lounge is a place where you can discuss anything that takes your fancy. If you just want to laze about and discuss things that don't quite fit elsewhere, then this is the place." BTW: I use several API functions for e.g. IsBad****(...) to know the status of objects and functions. HTH :) ;) ;P :-D :cool: Farhan Noor Qureshi

                T Offline
                T Offline
                Tim Lesher
                wrote on last edited by
                #7

                Farhan Noor Qureshi wrote: Please post all programming related questions in their respective forums. Please read the heading on the lounge, it states "The Lounge is a place where you can discuss anything that takes your fancy. If you just want to laze about and discuss things that don't quite fit elsewhere, then this is the place." Isn't this a contradiction? If "The Lounge is a place where you can discuss anything..., doesn't the set of all subjects contain the set of subjects dealing with coding? Just being pedantic. ;-) Tim Lesher http://www.lesher.ws

                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