Wednesday, November 15, 2023

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

}

 

0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top