我正在开发一个小的c++程序并学习异常。下面的代码是“坏的”吗?如果是的话,我能做些什么来改进它呢?
try {
// code
if (some error) {
throw "Description of error.";
}
}
catch (char* errorMessage) {
cerr << errorMessage << endl << "Fatal error";
}将char数组作为异常抛出有什么问题吗?
编辑:这是一种更好的方式吗?
const char errorMessage[] = "Description of error";
try {
// code
if (some error) {
throw errorMessage;
}
}
catch (char* errorMessage) {
cerr << errorMessage << endl << "Fatal error";
}发布于 2014-02-21 11:00:09
我认为这要简单得多。:)。
#include <iostream>
#include <exception>
using namespace std;
int main() {
try {
throw runtime_error("This is an Error");
}catch (exception& e){
cout << "Exception: " << e.what() << endl;
}
return 0;
}https://stackoverflow.com/questions/6248404
复制相似问题