c++错误处理分为两类,一类是逻辑错误logic_error,另一类是运行时错误runtime_error,
它们都继承exception,,同时逻辑错误下面又有子类domain_error, invalid_argument, length_error, out_of_range,runtime_error子类有range_error,overflow_error,underflow_error.
它们的继承结构图如下
#include <iostream> //std::cout, std::ios
#include <stdexcept>
#include <fstream> //std::ifstream
#include <string> //std::string
#include <vector>
#include <future> // std::promise, std::future_error
#include <bitset>
using std::cout;
using std::ios;
using std::endl;
using std::ifstream;
using std::string;
using std::vector;
using std::domain_error;
double getFloatArg(const char *arg)
{
char *endptr;
double x = strtod(arg, &endptr);
if (endptr == arg)
{
cout << "Error processing argument: " << arg << endl;
throw domain_error("bad argument");
}
return x;
}
int main()
{
//1.domain_error
try {
char szOrbits[] = "asc.24 29.53";
double dl;
dl = getFloatArg(szOrbits);
cout << "dl========= " << dl << endl;
} catch (const std::domain_error &e) {
const type_info &info = typeid(e);
cout << "info=== name = " << typeid(info).name() << " raw_name = " << typeid(info).raw_name() << " hash_code = " << typeid(info).hash_code() << endl;
cout << "Error: " << e.what() << endl;
cout << endl;
}
//2.invalid_argument
try {
// bitset constructor throws an invalid_argument if initialized
// with a string containing characters other than 0 and 1
std::bitset<5> mybitset (std::string("01234"));
}
catch (const std::invalid_argument& ia) {
std::cerr << "Invalid argument: " << ia.what() << '\n';
}
//3.length_error
try {
//vector throws a length_error if resized above max_size
vector<int> myvector;
myvector.resize(myvector.max_size()+1);
}
catch (const std::length_error& le) {
std::cerr << "Length error: " << le.what() << '\n';
}
//4.out_of_range
try {
std::vector<int> myvector(10);
myvector.at(20)=100; // vector::at throws an out-of-range
}
catch (const std::out_of_range& oor) {
std::cerr << "Out of Range error: " << oor.what() << '\n';
}
//5.future_error
try {
std::promise<int> prom;
prom.get_future();
prom.get_future(); // throws std::future_error
}
catch (std::future_error& e) {
cout << "future_error caught Code: " << e.code().value() << endl;
cout << "future_error caught Category: " << e.code().category().name() << endl;
cout << "future_error caught Message: " << e.code().message() << endl;
cout << "future_error caught default_error_condition: " << e.code().default_error_condition().message() << endl;
cout << "future_error caught bool: " << e.code().operator bool() << endl;
cout << "future_error caught what: " << e.what() << endl;
}
cout << "Hello World!" << endl;
return 0;
}
运行结果:
#include <iostream> //std::cout, std::ios
#include <stdexcept>
#include <fstream> //std::ifstream
#include <string> //std::string
#include <bitset>
using std::cout;
using std::ios;
using std::endl;
using std::ifstream;
using std::string;
using std::vector;
using std::domain_error;
using std::range_error;
using std::overflow_error;
using std::exception;
using std::bitset;
int main()
{
///runtime_error
//1.range_error
try
{
throw range_error("The range is in error!");
}
catch (const std::range_error& e)
{
std::cout << "Caught: " << e.what() << endl;
std::cout << "Type: " << typeid(e).name() << endl;
}
cout << endl;
//2.overflow_error
try
{
bitset<33> b;
b[32] = 1;
b[0] = 1;
unsigned long x = b.to_ulong();
cout << x << endl;
}
catch (const std::overflow_error& e)
{
cout << "Caught: " << e.what() << endl;
cout << "Type: " << typeid(e).name() << endl;
}
cout << endl;
//3.underflow_error
try
{
throw std::underflow_error("The number's a bit small, captain!");
}
catch (const std::underflow_error& e)
{
std::cerr << "Caught: " << e.what() << endl;
std::cerr << "Type: " << typeid(e).name() << endl;
}
cout << endl;
//4.system_error
ifstream is;
is.exceptions(std::ios::failbit);
try {
is.open("unexistent.txt");
} catch (const std::system_error &e) {
cout << "Exception caught (system_error):" << endl;
cout << "Error: " << e.what() << endl;
cout << "Code: " << e.code().value() << endl;
cout << "Category: " << e.code().category().name() << endl;
cout << "Message: " << e.code().message() << endl;
cout << "default_error_condition: " << e.code().default_error_condition().message() << endl;
cout << "bool: " << e.code().operator bool() << endl;
cout << endl;
}
cout << "Hello World!" << endl;
return 0;
}
运行结果:
参考:
https://zh.cppreference.com/w/cpp/header/stdexcept
https://vimsky.com/examples/detail/cpp-ex---std-domain_error-method.html