• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

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;
   }
}
0
prain
Asked:
prain
  • 5
  • 3
1 Solution
 
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;
}
0
 
prainAuthor Commented:
Mr. Genius jrk, please could you take a look at this?
0
 
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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<--------
0
 
prainAuthor Commented:
Ok. Thanks. But rstaveley, what is the diff between my program and your second program?
0
 
rstaveleyCommented:
throw /* new */ my_bad_alloc("bad_allocation");
0
 
prainAuthor Commented:
Ok I get it. Thanks.
0
 
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now