मैं ऑब्जेक्ट का उपयोग करने से खुद को कैसे बचा सकता हूं जो अपवादों का उपयोग करते समय पूरी तरह से नहीं बनाया गया है? क्या मुझे कन्स्ट्रक्टर में पकड़ना चाहिए? या शायद यह बुरा अभ्यास है? अगर मैं कन्स्ट्रक्टर ऑब्जेक्ट में पकड़ लेगा तो बनाया जाएगा।सी ++ कन्स्ट्रक्टर में पकड़ा अपवाद
#include <stdio.h>
class A
{
public:
A()
{
try {
throw "Something bad happened...";
}
catch(const char* e) {
printf("Handled exception: %s\n", s);
}
// code continues here so our bad/broken object is created then?
}
~A()
{
printf("A:~A()");
}
void Method()
{ // do something
}
};
void main()
{
A object; // constructor will throw... and catch, code continues after catch so basically we've got
// broken object.
//And the question here:
//
//* is it possible to check if this object exists without catching it from main?
// &object still gives me an address of this broken object so it's created but how can I protect myself
// from using this broken object without writing try/catch and using error codes?
object.Method(); // something really bad. (aborting the program)
};
आप अपने आप पर पूरी तरह से मनमानी प्रतिबंध क्यों लगाते हैं? –