assert: 断言
assert() 会对表达式expression
进行检测:
- 如果
expression
的结果为 0(条件不成立),那么断言失败,表明程序出错,assert() 会向标准输出设备(一般是显示器)打印一条错误信息,并调用 abort() 函数终止程序的执行。 - 如果
expression
的结果为非 0(条件成立),那么断言成功,表明程序正确,assert() 不进行任何操作。
try{} catch() : 异常,可以防止程序crash。
try{
} catch(const excption& e)
{
throw();
}
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main()
{
const std::map<uint8_t, std::vector<char>> channel_table = \
{
{0, {'a', 'b'}},
{1, {'c', 'd'}},
{2, {'e', 'f'}},
{3, {'g', 'h'}},
};
std::vector<char> mChar = channel_table.at(10);
/***
try
{
std::vector<char> mChar = channel_table.at(10);
}
catch (std::out_of_range& e)
{
std::cout << "Map table is out of range" << std::endl;
}
try
{
std::vector<char> mChar = channel_table.at(10);
}
catch (...)
{
std::cout << "Map table is out of range" << std::endl;
}
***/
std::cout << "The process is not crashed." << std::endl;
return 0;
}