简体   繁体   中英

C++, Standard library Exceptions

In C++, "new.h" the below class defined

class bad_alloc : public exception 
{
   public:
        bad_alloc() throw() { }
        virtual ~bad_alloc() throw();
};

Both constructors and destructors are explicitely specified to do not throw exceptions, it has no other member functions, but it inherits the "exception" class.

My assumption is the "new" operator usees this class to throw an exception when error occurs during allocation. But how? there is no valid member function, how it is actually used in the code?

I want to know, what is the usage of this class "bad_alloc" declaration? Please help me.

Member functions are not required - this is perfectly valid:

throw 42;

Mostly with exceptions we are interested in the type of the exception and not much else. However standard exceptions do inherit some functions from the exception base class.

Exceptions do not really need to have members. You declare the classes so that you can explicitly catch them:

try {
    Foo * foo = new Foo();
}
catch(std::bad_alloc &) {
    std::cout << "could not allocate" << std::endl;
}

The name of the type is all the information required. The above statement will catch only bad_alloc and all sub-classes thereof.

The valid member functions are inherited from exception . The purpose of bad_alloc is that you don't catch it when you're looking for a runtime_error or a logic_error .

there is no valid member function, how it is actually used in the code?

The purpose of this class is solely to signal (to the caller of new ) a failed allocation (= lack of free memory). It doesn't need any member functions.

An object of the class is created and then thrown: throw bad_alloc(); . Essentially you can throw any object or value. It's type can then be used in a catch() statement to grab only this kind of exception.

Example:

try
{
   // some code throwing exceptions
}
catch(const bad_alloc&)
{
    // this code is only run if an object of class bad_alloc is thrown
}
catch(const exception&)
{
    // this code is run if an object of class exception (or any unhandled derivated class) is thrown
}

You could as well ignore such classes and just throw strings (throw "Error happened.") but you can't differentiate them so you either handle all strings or none.

This is a code for new operator. Nothing special. It just throws an instance of this class.

#define _RAISE(x)   throw (x)

void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)
        {       // try to allocate size bytes
        void *p;
        while ((p = malloc(size)) == 0)
                if (_callnewh(size) == 0)
                {       // report no memory
                static const std::bad_alloc nomem;
                _RAISE(nomem);
                }

        return (p);
        }

It is an exception thrown in situations when some routine failed to allocate memory. It is used like this if you want to signal such a condition:

throw std::bad_alloc();

Also, if you want to catch memory allocation errors, it goes like this:

try {
    ...do something here...
} catch (const std::bad_alloc& ex) {
    ...print an error message or handle the exception in some other way...
}

A bad_alloc exception is thrown when new fails to allocate memory.

Classical C malloc() signalled a problem by returning a NULL pointer, but in C++ the new operator throws an exception instead. You can then catch the exception:

try {
    int * bigArray = new int[1024*1024*1024] ;
}
catch (bad_alloc& ex) { 
    cout<<"bad_alloc :"<<ex.what()<<endl ;
}

Note that you can use a special version of new that does not throws exception but returns a NULL pointer in case of error. You can use it by calling new (nothrow) instead of new

While asking for 4 gigs of memory will most likely result in a catchable std::bad_alloc exception, catching bad_alloc is in general a bit dicey.

try {
  char * one_byte = new char(42);
}
catch (std::bad_alloc & except) {
  // Doing anything but calling exit here will almost certainly fail.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM