bad_alloc: This is available in the file new. Type of the exceptions thrown by the standard definitions of operator new and operator new[] when they fail to allocate the requested storage space. This is used for exception handling.
Note: Exception is possibly thrown by new.
Approach
C++
#include <bits/stdc++.h>using namespace std;int main(){try{char *arr = new char[100];}catch (const std::bad_alloc &e){std::cerr << "Error has occured " << e.what() << '\n';}return 0;}
If the new does not allocate the memory to the array, then it will throw an exception and output: Error has occurred bad_alloc.
Otherwise, it will allocate memory for the array.
No comments:
Post a Comment