添加异常处理
This commit is contained in:
parent
fe19ca8831
commit
2256245596
@ -74,4 +74,5 @@ 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)
|
||||
add_executable(newexcp exception/newexcp.cpp)
|
||||
add_executable(use_sales exception/use_sales.cpp)
|
@ -7,6 +7,12 @@ using namespace std;
|
||||
|
||||
Sales::bad_index::bad_index(int ix, const std::string &s) :std::logic_error(s),bi(ix){
|
||||
}
|
||||
Sales::Sales(int yy) {
|
||||
year = yy;
|
||||
for (int i = 0; i <MONTHS ; ++i) {
|
||||
gross[i] = 0;
|
||||
}
|
||||
}
|
||||
Sales::Sales(int yy, const double *gr, int n) {
|
||||
year =yy;
|
||||
int lim = (n < MONTHS)?n : MONTHS;
|
||||
|
@ -31,7 +31,7 @@ public:
|
||||
|
||||
Sales(int yy, const double *gr, int n);
|
||||
|
||||
virtual ~Sales();
|
||||
virtual ~Sales(){};
|
||||
|
||||
int Year() const {
|
||||
return year;
|
||||
@ -64,7 +64,7 @@ public:
|
||||
};
|
||||
explicit LabeledSales(const std::string & lb = "none",int yy =0);
|
||||
LabeledSales(const std::string & lb,int yy,const double * gr,int n);
|
||||
virtual ~LabeledSales();
|
||||
virtual ~LabeledSales(){};
|
||||
const std::string & Label() const{
|
||||
return label;
|
||||
}
|
||||
|
59
base/exception/use_sales.cpp
Normal file
59
base/exception/use_sales.cpp
Normal file
@ -0,0 +1,59 @@
|
||||
//
|
||||
// Created by nicemoe on 2021/9/12.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include "sales.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
double vals1[12]{
|
||||
1220,1100,1122,2212,1232,2334,
|
||||
2884,2393,3302,2922,3002,2533
|
||||
};
|
||||
double vals2[12]{
|
||||
12,11,22,21,32,34,
|
||||
28,29,33,29,32,35
|
||||
};
|
||||
Sales sales1(2011,vals1,12);
|
||||
LabeledSales sales2("Blogstar",2012,vals2,12);
|
||||
cout <<"First try block:\n";
|
||||
try {
|
||||
int i;
|
||||
cout <<"Year = "<<sales1.Year() << endl;
|
||||
for (i = 0; i < 12; ++i) {
|
||||
cout << sales1[i] << ' ';
|
||||
if (i % 6 == 5) cout << endl;
|
||||
}
|
||||
cout <<" Year = "<< sales2.Year() << endl;
|
||||
cout <<" Label = "<< sales2.Label() << endl;
|
||||
for (i = 0; i <= 12; ++i) {
|
||||
cout << sales2[i] << ' ';
|
||||
if (i % 6 == 5) cout << endl;
|
||||
}
|
||||
cout <<"End of try block 1.\n";
|
||||
}catch (LabeledSales::nbad_index & bad){
|
||||
cout << bad.what();
|
||||
cout <<"Company: "<<bad.label_val() << endl;
|
||||
cout <<"bad index: "<<bad.bi_val()<<endl;
|
||||
}catch (Sales::bad_index & bad){
|
||||
cout << bad.what();
|
||||
cout <<"bad index: "<<bad.bi_val()<<endl;
|
||||
}
|
||||
cout<<"\nNext try block:\n";
|
||||
try{
|
||||
sales2[2] = 37.5;
|
||||
sales1[20] = 23345;
|
||||
cout <<"End of try block 2.\n";
|
||||
}catch (LabeledSales::nbad_index & bad){
|
||||
cout << bad.what();
|
||||
cout <<"Company: "<<bad.label_val() << endl;
|
||||
cout <<"bad index: "<<bad.bi_val()<<endl;
|
||||
}catch (Sales::bad_index & bad){
|
||||
cout << bad.what();
|
||||
cout <<"bad index: "<<bad.bi_val()<<endl;
|
||||
}
|
||||
cout <<"done\n";
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user