异常处理
illegalParameterValue.h :
#pragma once
#include<iostream>
using namespace std;
class illegalParameterValue
{
private:
string message;
public:
illegalParameterValue():
message("Illegal arameter value"){}
illegalParameterValue(string theMessage) {
message = theMessage;
}
void outputMessage() {
cout << message << "\n";
}
};
**Test.cpp : **
#include<iostream>
using namespace std;
#include"illegalParameterValue.h"
int abc(int a, int b, int c) {
if (a <= 0 || b <= 0 || c <= 0) {
throw illegalParameterValue("All parameters should be > 0");
}
return a + b * c;
}
int main() {
try {
cout<<abc(2, 0, 4)<<"\n";
}
catch(illegalParameterValue e){
cout << "The parameters to abc were 2,0,and 4" << "\n";
cout << "illegalParameterValue exception thrown" << "\n";
e.outputMessage();
}
return 0;
}