में कस्टम अपवाद बनाना मैं सी ++ सीख रहा हूं और जब मैं कोशिश करता हूं और अपना अपवाद बनाता हूं और उन्हें लिनक्स पर फेंकता हूं तो मुझे अनुभव होता है।सी ++
मैंने अपने कार्यान्वयन का परीक्षण करने के लिए एक छोटा परीक्षण प्रोजेक्ट बनाया है और नीचे मेरा अपवाद वर्ग हैडर फ़ाइल है।
class TestClass : public std::runtime_error
{
public:
TestClass(char const* const message) throw();
virtual char const* what() const throw();
};
अपवाद वर्ग के लिए स्रोत फ़ाइल
using namespace std;
TestClass::TestClass(char const* const message) throw()
: std::runtime_error(message)
{
}
char const * TestClass::what() const throw()
{
return exception::what();
}
अपने मुख्य अनुप्रयोग में, मैं एक समारोह जो मेरे अपवाद फेंकता है और आज़माएं/कैच में यह फैल जाती है इस प्रकार बोल रहा हूँ है:
void runAFunctionAndthrow();
/*
*
*/
int main(int argc, char** argv) {
try
{
cout << "About to call function" << endl;
runAFunctionAndthrow();
}
catch (TestClass ex)
{
cout << "Exception Caught: " << ex.what() << endl;
}
return 0;
}
void runAFunctionAndthrow()
{
cout << "going to run now. oh dear I need to throw an exception" << endl;
stringstream logstream;
logstream << "This is my exception error. :(";
throw TestClass(logstream.str().c_str());
}
जब मैं चलाने मैं निम्नलिखित उत्पादन प्राप्त करने के लिए उम्मीद कर रहा हूँ:
About to call function
Going to run now. oh dear I need to throw an exception
Exception Caught: This is my exception error. :(
इसके बजाय मैं क्या मिल रहा है
About to call function
going to run now. oh dear I need to throw an exception
Exception Caught: std::exception
सूचना अंतिम पंक्ति यह कहता है std :: मेरे वास्तविक अपवाद संदेश के बजाय अपवाद नहीं है "यह मेरा अपवाद त्रुटि है"।
यह क्यों है, यह विंडोज़ पर ठीक काम करता है लेकिन लिनक्स पर यह करता है।
मैंने जो कुछ किया है, उससे मैंने जो कुछ देखा है, वह सही है, इसलिए मुझे क्या याद आ रहा है।
'क्या()' आप शायद मतलब 'वापसी runtime_error :: क्या() में,' - जो reimplementation पूरी तरह छोड़ माना जायेगा (आधार वर्ग व्यवहार यहाँ पहले से ही अच्छा आप के लिए है)। –