दोनों मामलों में, चूंकि आप संदर्भ से पकड़ते हैं, आप प्रभावी रूप से मूल अपवाद ऑब्जेक्ट की स्थिति को बदल रहे हैं (जिसे आप एक जादुई मेमोरी स्थान में रहने के रूप में सोच सकते हैं जो बाद में होने वाली अनिच्छा के दौरान मान्य रहेगा - 0x98e7058
नीचे दिए गए उदाहरण में)। तथापि,
- पहले मामले में, जब से आप के साथ पुनर्विचार करते हैं
throw;
(जो, इसके विपरीत throw err;
, अपने संशोधनों के साथ, मूल अपवाद ऑब्जेक्ट को संरक्षित करता है, तो "जादुई स्थान" पर 0x98e7058
) ने कहा कि अपील में कॉल को प्रतिबिंबित करेगा ()
- दूसरे मामले में, के बाद से आप स्पष्ट रूप से कुछ फेंक, एक प्रतिलिपि की
err
जा बनाया तो एक अलग "जादुई स्थान" पर नए सिरे से फेंक दिया जाएगा ( 0x98e70b0
- क्योंकि सभी के लिए संकलक जानता err
ढेर पर एक वस्तु, हो सकता है के बारे में unwinded होने के लिए e
था पर 0xbfbce430
, "जादुई स्थान" पर नहीं 0x98e7058
), इसलिए आप बेस क्लास उदाहरण की कॉपी-निर्माण के दौरान व्युत्पन्न वर्ग-विशिष्ट डेटा खो देंगे ।
यह बताने के लिए कि क्या हो रहा है: सरल कार्यक्रम
#include <stdio.h>
struct MyErr {
MyErr() {
printf(" Base default constructor, this=%p\n", this);
}
MyErr(const MyErr& other) {
printf(" Base copy-constructor, this=%p from that=%p\n", this, &other);
}
virtual ~MyErr() {
printf(" Base destructor, this=%p\n", this);
}
};
struct MyErrDerived : public MyErr {
MyErrDerived() {
printf(" Derived default constructor, this=%p\n", this);
}
MyErrDerived(const MyErrDerived& other) {
printf(" Derived copy-constructor, this=%p from that=%p\n", this, &other);
}
virtual ~MyErrDerived() {
printf(" Derived destructor, this=%p\n", this);
}
};
int main() {
try {
try {
MyErrDerived e;
throw e;
} catch (MyErr& err) {
printf("A Inner catch, &err=%p\n", &err);
throw;
}
} catch (MyErr& err) {
printf("A Outer catch, &err=%p\n", &err);
}
printf("---\n");
try {
try {
MyErrDerived e;
throw e;
} catch (MyErr& err) {
printf("B Inner catch, &err=%p\n", &err);
throw err;
}
} catch (MyErr& err) {
printf("B Outer catch, &err=%p\n", &err);
}
return 0;
}
परिणाम:
Base default constructor, this=0xbfbce430
Derived default constructor, this=0xbfbce430
Base default constructor, this=0x98e7058
Derived copy-constructor, this=0x98e7058 from that=0xbfbce430
Derived destructor, this=0xbfbce430
Base destructor, this=0xbfbce430
A Inner catch, &err=0x98e7058
A Outer catch, &err=0x98e7058
Derived destructor, this=0x98e7058
Base destructor, this=0x98e7058
---
Base default constructor, this=0xbfbce430
Derived default constructor, this=0xbfbce430
Base default constructor, this=0x98e7058
Derived copy-constructor, this=0x98e7058 from that=0xbfbce430
Derived destructor, this=0xbfbce430
Base destructor, this=0xbfbce430
B Inner catch, &err=0x98e7058
Base copy-constructor, this=0x98e70b0 from that=0x98e7058
Derived destructor, this=0x98e7058
Base destructor, this=0x98e7058
B Outer catch, &err=0x98e70b0
Base destructor, this=0x98e70b0
और देखें: