添加异常处理

This commit is contained in:
朱毅骏 2021-09-07 18:56:54 +08:00
parent 1bd6f7e845
commit dc6c5a0106
7 changed files with 280 additions and 1 deletions

View File

@ -69,4 +69,9 @@ add_executable(tempmemb2 stl/tempmemb2.cpp)
add_executable(tempparm stl/tempparm.cpp)
add_executable(frnd2tmp stl/frnd2tmp.cpp)
add_executable(tmp2tmp stl/tmp2tmp.cpp)
add_executable(manyfmd stl/manyfmd.cpp)
add_executable(manyfmd stl/manyfmd.cpp)
add_executable(error2 exception/error2.cpp)
add_executable(error3 exception/error3.cpp)
add_executable(exc_mean exception/exc_mean.cpp)
add_executable(error5 exception/error5.cpp)
add_executable(newexcp exception/newexcp.cpp)

33
base/exception/error2.cpp Normal file
View File

@ -0,0 +1,33 @@
//
// Created by nicemoe on 2021/9/5.
//
#include <iostream>
#include <cfloat>
using namespace std;
bool hmean(double a, double b, double *ans);
int main() {
double x, y, z;
cout << "Enter two numbers: ";
while (cin >> x >> y) {
if (hmean(x, y, &z))cout << "Harmonice mean of " << x << " and " << y << " is " << z << endl;
else cout <<"One value should not be the negative of the other - try again.\n";
cout << "Enter next set of numbers <q to quit>:";
}
cout << "Bye!\n";
return 0;
}
bool hmean(double a, double b, double *ans) {
if (a == -b) {
*ans = DBL_MAX;
return false;
}
*ans = 2.0 * a * b / (a + b);
return true;
}

35
base/exception/error3.cpp Normal file
View File

@ -0,0 +1,35 @@
//
// Created by nicemoe on 2021/9/5.
//
#include <iostream>
using namespace std;
double hmean(double a, double b);
int main() {
double x, y, z;
cout << "Enter two numbers: ";
while (cin >> x >> y) {
try {
z = hmean(x, y);
}catch (const char * s){
cout<<s<<endl;
cout <<"Enter a new pair numbers: ";
continue;
}
cout << "Harmonice mean of " << x << " and " << y << " is " << z << endl;
cout << "Enter next set of numbers <q to quit>:";
}
cout << "Bye!\n";
return 0;
}
double hmean(double a, double b) {
if (a == -b) {
throw "bod hmean() arguments: a = -b not allowed";
}
return 2.0 * a * b / (a + b);
}

95
base/exception/error5.cpp Normal file
View File

@ -0,0 +1,95 @@
//
// Created by zhuyijun on 2021/9/7.
//
#include <iostream>
#include <cmath>
#include <string>
#include "exc_mean.h"
using namespace std;
class demo {
private:
string word;
public:
demo(const string &str) {
word = str;
cout << "demo " << word << " created\n";
}
~demo() {
cout << "demo " << word << " destroyed\n";
}
void show() const {
cout << "demo " << word << " lives!\n";
}
};
double hmean(double a, double b);
double gmean(double a, double b);
double means(double a, double b);
int main() {
double x, y, z;
{
demo d1("found in block in main()");
cout << "Enter two numbers: ";
while (cin >> x >> y) {
try {
z = means(x, y);
cout << "The mean mean of " << x << " and " << y << " is " << z << endl;
cout << "Enter next pair: ";
} catch (bad_hmean &bh) {
bh.mesg();
cout << "Try again.\n";
continue;
} catch (bad_gmean &bg) {
cout << bg.mesg();
cout << "Values used: " << bg.v1 << " , " << bg.v2 << endl;
cout << "Sorry,you don't get to play any more.\n";
break;
} catch (...) {
cout<<"出现异常"<<endl;
}
}
d1.show();
}
cout << "Bye!\n";
cin.get();
cin.get();
return 0;
}
double hmean(double a, double b) {
if (a == -b)
throw bad_hmean(a, b);
return 2.0 * a * b / (a + b);
}
double gmean(double a, double b) {
if (a < 0 || b < 0)
throw bad_gmean(a, b);
return sqrt(a * b);
}
double means(double a, double b) {
double am, hm, gm;
demo d2("found in means()");
am = (a + b) / 2.0;
try {
hm = hmean(a, b);
hm = gmean(a, b);
} catch (bad_hmean &bh) {
bh.mesg();
cout << "Caught in means()\n";
throw;
} catch (...) {
cout<<"出现异常"<<endl;
}
d2.show();
return (am + hm + gm) / 3.0;
}

View File

@ -0,0 +1,48 @@
//
// Created by zhuyijun on 2021/9/7.
//
#include "exc_mean.h"
#include <cmath>
using namespace std;
double hmean(double a, double b);
double gmean(double a, double b);
int main() {
double x, y, z;
cout << "Enter two numbers: ";
while (cin >> x >> y) {
try {
z = hmean(x, y);
cout << "Harmonic mean of " << x << " and " << y << " is " << z << endl;
cout << "Geometric mean of " << x << " and " << y << " is " << gmean(x, y) << endl;
cout << "Enter next set of numbers <q to quit>:";
} catch (bad_hmean &bh) {
bh.mesg();
cout << "Try again.\n";
continue;
} catch (bad_gmean &bg) {
cout << bg.mesg();
cout << "Values used: " << bg.v1 << " , " << bg.v2 << endl;
cout << "Sorry,you don't get to play any more.\n";
break;
}
}
cout << "Bye!\n";
return 0;
}
double hmean(double a, double b) {
if (a == -b)
throw bad_hmean(a, b);
return 2.0 * a * b / (a + b);
}
double gmean(double a, double b) {
if (a < 0 || b < 0)
throw bad_gmean(a, b);
return sqrt(a * b);
}

33
base/exception/exc_mean.h Normal file
View File

@ -0,0 +1,33 @@
//
// Created by zhuyijun on 2021/9/7.
//
#ifndef BASE_EXC_MEAN_H
#define BASE_EXC_MEAN_H
#include <iostream>
class bad_hmean {
private:
double v1;
double v2;
public:
bad_hmean(double a=0,double b=0):v1(a),v2(b){}
void mesg();
};
inline void bad_hmean::mesg() {
std::cout<<"hmean("<<v1<<", "<<v2<<") : " <<"invalid arguments:a = -b\n";
}
class bad_gmean{
public:
double v1;
double v2;
bad_gmean(double a=0,double b=0):v1(a),v2(b){}
const char * mesg();
};
inline const char * bad_gmean::mesg() {
return "gmean() arguments should be >= 0\n";
}
#endif //BASE_EXC_MEAN_H

View File

@ -0,0 +1,30 @@
//
// Created by zhuyijun on 2021/9/7.
//
#include <iostream>
#include <new>
#include <cstdlib>
using namespace std;
struct Big{
double stuff[20000];
};
int main() {
Big * pb;
try {
cout <<"Trying to get a big block of memory:\n";
pb = new Big[100000];
cout<<"Got past the new request:\n";
}catch (bad_alloc & ba){
cout <<"Caught the exception\n";
cout <<ba.what()<<endl;
}
cout <<"Memory successfully allocated\n";
pb[0].stuff[0] = 4;
cout << pb[0].stuff[0] << endl;
delete []pb;
return 0;
}