???????????????????????????????????£?????????????????????漸??????:
????· ???????????????????????????????????漸???????????
????· ???????????????????????????????????????????????????????catch???????????????汾
????· ???????????????????????
????· ???????鱻???????????????????????????????
???????????????????????????????????????????????????????????????????????????????д???????catch????????п?????????г???????????????????????????????????????????????C++?????????????κ???????????????????д????е?????
????catch(...) {
????//????????????????????κ?????????????????????????????
????}
????????????????????????????????????е??Щ???????????????????????????????Щ?????????????????????????????????????????????????????C++?????????
????try{
????throw Exception("I am a exception");
????}catch(...) {
????//log the exception
????throw;
????}
?????????catch?????м??????throw?????????????????????????????????????????У???????????????????????????????????κ?catch???????????????????????????????????????????????
????terminate called after throwing an instance of 'MyError'
????Aborted (core dumped)
??????????????????????????????????????????????????????????ù?????????????????????????????????????????б????????μ???terminate???????????????????????terminate???????μ??????????????????C++????set_terminate?????????????????????terminate??????????????????????????б????????????terminate???????д?????????????????????:
#include <exception>
#include <iostream>
#include <cstdlib>
using namespace std;
class MyError {
const char* const data;
public:
MyError(const char* const msg = 0):data(msg)
{
//idle
}
};
void do_error() {
throw MyError("something bad happend");
}
//??????terminate???????????????????
void terminator()
{
cout << "I'll be back" << endl;
exit(0);
}
int main()
{
//??????????terminate???????????е?terminate???????
void (*old_terminate)() = set_terminate(terminator);
do_error();
}
????????????????I'll be back
???????????????????????????????????????????????????????????????????????е?????????
???????е????????
?????????????????????????????????????????????????????????й???????C++?е????????????????C++????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????:
#include <iostream>
#include <string>
using namespace std;
class base
{
public:
base()
{
cout << "I start to construct" << endl;
if (count == 3) //??????????????????
throw string("I am a error");
count++;
}
~base()
{
cout << "I will destruct " << endl;
}
private:
static int count;
};
int base::count = 0;
int main()
{
try{
base test[5];
} catch(...){
cout << "catch some error" << endl;
}
}
????????????????????:
????I start to construct
????I start to construct
????I start to construct
????I start to construct
????I will destruct
????I will destruct
????I will destruct
????catch some error
???????????????й????????????????????????????????????У?????????????????????????????????????????а????????????????????ж?????в?????д?????????????????????try???飬???main????????????????????????????????????е??????б?????????????????????????????????????????try??????÷?:
????#include <iostream>
????using namespace std;
????int main() try {
????throw "main";
????} catch(const char* msg) {
????cout << msg << endl;
????return 1;
????}
????main???????飬???????main???????????????
class Base
{
public:
Base(int data??string str)try:m_int(data)??m_string(str)//???????б??п???????????????в??
{
// some initialize opt
}catch(const char* msg) {
cout << "catch a exception" << msg << endl;
}
private:
int m_int;
string m_string;
};
int main()
{
Base base(1??"zhangyifei");
}
????????????????????????????ζ??????????????д?????????????????????????????????????????????е??????????????????
?????????
????C++?????????????????е?????????Щ??????????exception?????????????????????????????????logic_error???????????runtime_error??????????stdexcept?????У??????????????????г??????????????紫??????Ч??????????????????Щ??????????????????????????????????????????????????????????????std::string???????????????????????????????????????what?????????????????
#include <stdexcept>
#include <iostream>
#include <string>
using namespace std;
class MyError:public runtime_error {
public:
MyError(const string& msg = "") : runtime_error(msg) {}
};
//runtime_error logic_error ??????????????????????string??????
//
int main()
{
try {
throw MyError("my message");
} catch(MyError& x) {
cout << x.what() << endl;
}
}
????????????
??????????????????????Щ?????????????????????е??Щ?????????????????????????????????C++?????????????????????????????????г????????????????д????????ο?????????????????????C++11???????????????????????????????????????????????????????????????????÷???????????C++11???????????????:
#include <exception>
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
class Up{};
class Fit{};
void g();
//??????????f??????????Up ??Fit???????
void f(int i)throw(Up??Fit) {
switch(i) {
case 1: throw Up();
case 2: throw Fit();
}
g();
}
void g() {throw 47;}
void my_ternminate() {
cout << "I am a ternminate" << endl;
exit(0);
}
void my_unexpected() {
cout << "unexpected exception thrown" << endl;
// throw Up();
throw 8;
//?????unexpected?м???????????????????????е??????????????????
//???????????????????????з????????
//1.????????????bad_exception ???????????????bad_exception
//2.?????????????bad_exception ??????3??????ternminate????
// exit(0);
}
int main() {
set_terminate(my_ternminate);
set_unexpected(my_unexpected);
for(int i = 1;i <=3;i++)
{
//??????????????????????????е????
//?????????????unexpected?????????set_unexpected????
//?????????????unexpected??????
try {
f(i);
}catch(Up) {
cout << "Up caught" << endl;
}catch(Fit) {
cout << "Fit caught" << endl;
}catch(bad_exception) {
cout << "bad exception" << endl;
}
}
}
}
?????????????????????????????????????unexpected???????????????????????????unexpected????????????????unexpected?????м??????????????£?????δ??????????.C++11??????????????????????????????noexcept?????????????????????????????
????void recoup(int) noexecpt(true);  //recoup?????????
????void recoup(int) noexecpt(false); //recoup??????????
????????????noexecpt????????????????????????
?????????
????????????????????????????????????????????????????溯???????????????????£????????μ??????????????????????????????????????????е????????????????top????????????????????????????????void??pop???????????????????????????????????pop???????? ?????????????????????????????????
????template<typename T> T stack<T>::pop()
????{
????if(count == 0)
????throw logic_error("stack underflow");
????else
????return data[--count];
????}
???????????????????????????????????????????н?????????????????Count????????????????????????????????????????????????????????????????????£?1.???????2.????????????y?????????????????????????????????У??????????????????????????????£????? ????????????????????????????????????????????д??????α???????????????????
????class Bitmap {...};
????class Widget {
????...
????private:
????Bitmap *pb;
????};
????Widget& Widget::operator=(const Widget& rhs)
????{
????delete pb;
????pb = new Bitmap(*rhs.pb);
????return *this;
????}
???????????????????????????????rhs?????????????????*rhs.pb??????????????????????????£?????????????
????Widget& Widget::operator=(const Widget& rhs)
????{
????If(this == rhs) return *this; //???????
????delete pb;
????pb = new Bitmap(*rhs.pb);
????return *this;
????}
???????????????????????????????????????????delete pb????????????new Bitmap??????????????????????????鱻???????森??????????????£?????????????????????????
????Widget& Widget::operator=(const Widget& rhs)
????{
????If(this == rhs) return *this; //???????
????Bitmap *pOrig = pb;
????pb = new Bitmap(*rhs.pb); //???????T???????????????????this???????
????delete pOrig;
????return *this;
????}
?????????????????????????????????????????????????????????????????????????????