Is this This MSDN library exception excample is legitimate?

Hello, this is the example given by MSDN library on how to trigger the bad_alloc exception. If the exception bad_alloc is thrown it should be handled
in the catch area. But when I run the program it does not do anything. In fact it even does not print the print statement "reached the handler"
which I inserted. This tells me that the exception bad_alloc was NEVER raised in the new char[]. The example does not use {nothrow} clause
in the new(). So this should throw the bad_alloc. But it does not. Can any one point out why this behaves in a different way than expected?

Or rather is Microsoft is publishing a illegitimate example?

#include<new>
#include<iostream>
using namespace std;

int main() {
   char* ptr;
   try {
      ptr = new char[(~unsigned int((int)0)/2) - 1];
      delete[] ptr;
   }
   catch( bad_alloc &ba) {
         cout << "reached the handler" << endl;
      cout << ba.what( ) << endl;
   }
}
prainAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

prainAuthor Commented:
Further,
I created my own bad_alloc class called my_bad_alloc and overrode the what().
And then I threw an exception object within the main. Still the same. In fact this time (in Windows env.) it throws a system exception abort window
than catching the exception in my handler and displaying the string there.

Could an expert please try this example?.

#include <iostream>
 
using namespace std;
class my_bad_alloc : public bad_alloc
{
      char * _iMessage;
public:
      my_bad_alloc(char *message)
      {
            _iMessage = message;
      }
      virtual const char *what() const
      {
            return _iMessage;
      }
};
 

int main() {
   
   try {
      throw new my_bad_alloc("bad_allocation");
       
   }
   catch( bad_alloc &ba) {
         cout << "reached the handler" << endl;
      cout << ba.what( ) << endl;
   }

   return 0;
}
prainAuthor Commented:
Mr. Genius jrk, please could you take a look at this?
prainAuthor Commented:
I am surprised that no one is interested answering this question. In Java area however, if a question of this nature is posted, it will flood with answers.
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

rstaveleyCommented:
Your first example works fine for me VC 7.1.

With reference to your furthermore, you need to catch a pointer when you throw a pointer.

either:
--------8<--------
using namespace std;
class my_bad_alloc : public bad_alloc
{
     char * _iMessage;
public:
     my_bad_alloc(char *message)
     {
          _iMessage = message;
     }
     virtual const char *what() const
     {
          return _iMessage;
     }
};


int main() {

   try {
      throw new my_bad_alloc("bad_allocation");

   }
   //catch( bad_alloc &ba) {
   catch(bad_alloc* ba) {
        cout << "reached the handler" << endl;
      cout << ba->what( ) << endl;
   }

   return 0;
}
--------8<--------

or:
--------8<--------
#include <iostream>

using namespace std;
class my_bad_alloc : public bad_alloc
{
     char * _iMessage;
public:
     my_bad_alloc(char *message)
     {
          _iMessage = message;
     }
     virtual const char *what() const
     {
          return _iMessage;
     }
};


int main() {

   try {
      throw my_bad_alloc("bad_allocation");

   }
   catch(bad_alloc& ba) {
        cout << "reached the handler" << endl;
      cout << ba.what( ) << endl;
   }

   return 0;
}
--------8<--------

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
prainAuthor Commented:
Ok. Thanks. But rstaveley, what is the diff between my program and your second program?
rstaveleyCommented:
throw /* new */ my_bad_alloc("bad_allocation");
prainAuthor Commented:
Ok I get it. Thanks.
rstaveleyCommented:
You are creating an automatic rather than a dynamic object (i.e. it is created on the stack rather than heap) and you are throwing the object by value rather than by address.

When you throw an object by value, you can catch it by reference or by value. When you throw an object by address (i.e. pointer) you can only catch it by pointer.

In Java you can only create dynamic non-primitive objects which simplifies life, but you'll find that it restricts you in other ways. The alternatives available to you in C++ will at first seem bewildering, but you'll find that they give it its power too.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.