***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***

Saturday, July 24, 2021

Handling the bad_alloc Exception

When the new operator fails to allocate memory, C++ throws a bad_alloc exception. The bad_alloc exception type is defined in the new header file, so any program that attempts to catch this exception should have the following directive:

 #include <new.h>

 Here is the general format of a try/catch construct that catches the bad_alloc exception:

 

try

{

// Code that uses the new operator

}

catch (bad_alloc)

{

// Code that responds to the error

}

 

No comments:

Post a Comment