??????????
?????????????????????????????????????????????C?????в????????????????????????????????????????????????????????????д????????????????????????????????á?C++??????????????????????C++????????????????????????????????????????????????????????Щ???????:
???????????????д?????????ζ?????????????????????????????????????д????????????????????????????????????д??????????????ε???????????????????????????д??δ????????
??????????????????????????????????????????δ?????????????????????????????????
?????????????????????
????????????????????????????C?????е???????????????????о???????????:
????· ??????з?????????????????????????????????
????· ???????????????????????????raise???????signal????????????????????????????????????????????????????????????
????· ?????C???е??????????? setjmp??longjmp ?????????setjmp??longjmp?????????ν??д?????:
#include <iostream>
#include <setjmp.h>
jmp_buf static_buf; //??????????????????????????
void do_jmp()
{
//do something??simetime occurs a little error
//????longjmp????????static_buf????????????????????????????????setjmp?????????????
longjmp(static_buf??10);//10??????????????????????????????????
}
int main()
{
int ret = 0;
//??????????????浽static_buf?У???????0?????????????????????????????????????
if((ret = setjmp(static_buf)) == 0) {
//???е????
do_jmp();
} else { //?????????
if (ret == 10)
std::cout << "a little error" << std::endl;
}
}
?????????????????????????????????????????????????????????????????????????????????????????????????????????C++????????????????????????????????????????????????????????????????????????????????????й????????????????????????????
#include <iostream>
#include <csetjmp>
using namespace std;
class base {
public:
base() {
cout << "base construct func call" << endl;
}
~base() {
cout << "~base destruct func call" << endl;
}
};
jmp_buf static_buf;
void test_base() {
base b;
//do something
longjmp(static_buf??47);//???????????????????b?????????
}
int main() {
if(setjmp(static_buf) == 0) {
cout << "deal with some thing" << endl;
test_base();
} else {
cout << "catch a error" << endl;
}
}
????????????δ????У????base?????????????????longjmp???????????b?????????????????????????????????????????b???????????????????????????????C++?????????????????????Щ??????C++????????????Щ????????????????????????ζ?????????????????????????????????.
???????????
class MyError {
const char* const data;
public:
MyError(const char* const msg = 0):data(msg)
{
//idle
}
};
void do_error() {
throw MyError("something bad happend");
}
int main()
{
do_error();
}
??????????????У????throw????????????????????????????????κ??????????????????????????????????????????????????????????????????????????????????????????????????????????н??л???????????????????????????????ɡ?????????????????????????????????д??????????????t?????????????????????????????????????У???????????????????????
???????????
????C++?????catch????????????????????????????????д????????????????????????????????????????????????????:
????try{
????//do something
????throw string("this is exception");
????} catch(const string& e) {
????cout << "catch a exception " << e << endl;
????}
????catch?е??????????????????????throw????????????????????????????????catch??????????????????????????????????????????????????????try???????п???????????????????????δ??????????????????catch?????????????????????????????????ν??????
???????????
?????????????????????????????????????????????Щ????????????????????????????????????????????????в???????????????????????????????????:
????#include <iostream>
????using namespace std;
????int main()
????{
????try{
????throw 'a';
????}catch(int a) {
????cout << "int" << endl;
????}catch(char c) {
????cout << "char" << endl;
????}
????}
?????????????????????char??????????????????char????????????????????????????????????????????з???????????????char????int??????????????????????????????????????????????????????????????ж?????Ч?????????????catch???β???????????????????????????????? ?????и??????????????
//????
classBase{
public:
Base(stringmsg):m_msg(msg)
{
}
virtualvoidwhat(){
cout<<m_msg<<endl;
}
voidtest()
{
cout<<"IamaCBase"<<endl;
}
protected:
stringm_msg;
};
//????????????????麯??
classCBase:publicBase
{
public:
CBase(stringmsg):Base(msg)
{
}
voidwhat()
{
cout<<"CBase:"<<m_msg<<endl;
}
};
intmain()
{
try{
//dosomething
//????????????
throwCBase("IamaCBaseexception");
}catch(Base&e){//????????????
e.what();
}
}
???????????δ???????????????????????????????д???????????????????????????б????????????????????????????????Base&????Base???????????????и???????????δ???????????????CBase???и????????CBase?е?test????????????á?
????try {
????//do some thing
????throw CBase("I am a CBase exception");
????}catch(Base e) {
????e.test();
????}