C++提供了在命名空间std的头文件
头文件:
#include
以下是在C++中引发的错误:
Exceptions | Description |
---|---|
bad_alloc | thrown by new on allocation failure |
bad_cast | thrown by dynamic_cast when it fails in a dynamic cast |
bad_exeption | thrown by certain dynamic exception specifiers |
bad_typeid | thrown by typeid |
bad_function_call | thrown by empty function objects |
bad_weak_ptr | thrown by shared_ptr when passed a bad weak_ptr |
logic_error | error related to the internal logic of the program |
runtime_error | error related to the internal logic of the program |
下面是说明C++中异常类中某些错误的程序:
程序1:
下面是对std :: bad_alloc错误的说明:
using class bad_alloc
// C++ program to illustrate bad_alloc
// using class bad_alloc
#include
#include
using namespace std;
// Function to illustrate bad_alloc
void createArray(int N)
{
// Try Block
try {
// Create an array of length N
int* array = new int[N];
// If created successfully then
// print the message
cout << "Array created successfully"
<< " of length " << N << " \n";
}
// Check if the memory
// was allocated or not
// using class bad_alloc
catch (bad_alloc& e) {
// If not, print the error message
cout << e.what()
<< " for array of length "
<< N << " \n";
}
}
// Driver Code
int main()
{
// Function call to create an
// array of 1000 size
createArray(1000);
// Function call to create an
// array of 1000000000 size
createArray(1000000000);
return 0;
}
using class exception
// C++ program to illustrate bad_alloc
// using class exception
#include
#include
using namespace std;
// Function to illustrate bad_alloc
void createArray(int N)
{
// Try Block
try {
// Create an array of length N
int* array = new int[N];
// If created successfully then
// print the message
cout << "Array created successfully"
<< " of length " << N << " \n";
}
// Check if the memory
// was allocated or not
// using class exception
catch (exception& e) {
// If not, print the error message
cout << e.what()
<< " for array of length "
<< N << " \n";
}
}
// Driver Code
int main()
{
// Function call to create an
// array of 1000 size
createArray(1000);
// Function call to create an
// array of 1000000000 size
createArray(1000000000);
return 0;
}
输出:
Array created successfully of length 1000
std::bad_alloc for array of length 1000000000
解释:
- 对于创建长度为1000的数组,内存分配成功,并且没有引发异常。
- 对于创建长度为1000的数组,内存分配未成功,并且引发了异常“ std :: bad_alloc” 。引发的异常类型为bad_alloc ,它是从类异常派生的。函数what()是基类异常中定义的虚函数。函数what()返回以null结尾的字符串,该字符串通常是错误的描述。
注意:当内存分配失败时,运算符“ new”会抛出bad_alloc异常。
为什么我们通过引用捕获异常?
通过值捕获异常将调用复制构造函数并创建该异常的副本,这会增加运行时开销。因此,通过引用进行捕获是更好的选择。如果我们要修改异常或在错误消息中添加一些其他信息,那么最好通过引用进行捕获。对于这种情况:
catch (std::string str)
{
s += "Additional info";
throw;
}
上面的程序想要捕获异常,向它添加一些信息,然后重新抛出它。但是str是一个按值调用的变量,该变量在函数中进行了本地更改,当函数重新引发异常时,将传递原始异常。
正确的代码:
catch (std::string& s)
{
s += "Additional info";
throw;
}
程式2:
下面是说明逻辑错误的程序:
// C++ program to illustrate logic_error
#include
#include
using namespace std;
// Function to find factorial of N and
// throws error if occurs
void findFactorial(int N)
{
// Initialise variable by 1
int factorial = 1;
// Check for errors
try {
// If N is less than zero then,
// it shows errors as factorial
// of negative number can't be
// calculated
if (N < 0) {
// Exception object which
// returns the message passed
// to it
throw invalid_argument(
"negative not allowed");
}
// Find factorial if no error occurs
for (int i = N; i > 0; i--) {
factorial *= i;
}
cout << "Factorial of " << N
<< " is " << factorial << endl;
}
// Print the error message
catch (exception& e) {
cout << e.what();
}
}
// Driver Code
int main()
{
// Function call to find factorial
// of 0
findFactorial(0);
// Function call to find factorial
// of 3
findFactorial(3);
// Function call to find factorial
// of -1
findFactorial(-1);
return 0;
}
输出:
Factorial of 0 is 1
Factorial of 3 is 6
negative not allowed
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。