तो मेरे पास अपवादों से निपटने और उनके वर्तमान पता पुस्तिका कार्यक्रम में उनका उपयोग करने वाला एक आगामी असाइनमेंट है जो अधिकांश होमवर्क केंद्रित है। मैंने अपवादों और पूरे प्रयास को पकड़ने की कोशिश की, और एक क्लास डिज़ाइन का उपयोग करने का फैसला किया, जो मुझे अंततः कुछ हफ्तों में मेरे असाइनमेंट के लिए करना होगा। मेरे पास ऐसा कोड है जो अपवाद को ठीक से जांचता है, लेकिन जो मैं जानना चाहता हूं, वह है कि अगर मेरा त्रुटि संदेश फ़ंक्शन मानकीकृत करने का कोई तरीका है, (यानी मेरा क्या() कॉल):अपवाद कैसे बनाएं?
यहां मेरा कोड है:
#include <iostream>
#include <exception>
using namespace std;
class testException: public exception
{
public:
virtual const char* what() const throw() // my call to the std exception class function (doesn't nessasarily have to be virtual).
{
return "You can't divide by zero! Error code number 0, restarting the calculator..."; // my error message
}
void noZero();
}myex; //<-this is just a lazy way to create an object
int main()
{
void noZero();
int a, b;
cout << endl;
cout << "Enter a number to be divided " << endl;
cout << endl;
cin >> a;
cout << endl;
cout << "You entered " << a << " , Now give me a number to divide by " << endl;
cin >> b;
try
{
myex.noZero(b); // trys my exception from my class to see if there is an issue
}
catch(testException &te) // if the error is true, then this calls up the eror message and restarts the progrm from the start.
{
cout << te.what() << endl;
return main();
}
cout <<endl;
cout << "The two numbers divided are " << (a/b) << endl; // if no errors are found, then the calculation is performed and the program exits.
return 0;
}
void testException::noZero(int &b) //my function that tests what I want to check
{
if(b == 0) throw myex; // only need to see if the problem exists, if it does, I throw my exception object, if it doesn't I just move onto the regular code.
}
मैं क्या करने में सक्षम होना चाहता हूं, ऐसा इसलिए है कि मेरा() फ़ंक्शन किस प्रकार की त्रुटि पर कॉल किया जा रहा है, इस पर निर्भर करता है। तो उदाहरण के लिए, अगर मैं एक त्रुटि को बुला रहा था जो शीर्ष संख्या को देखता था, (ए), यह देखने के लिए कि यह शून्य था या नहीं, और यदि यह था, तो यह संदेश कहने के लिए सेट करेगा कि "आपके पास नहीं हो सकता शून्य का एक अंशक ", लेकिन अभी भी() फ़ंक्शन के अंदर होना चाहिए। यहाँ एक उदाहरण है:
virtual const char* what() const throw()
if(myex == 1)
{
return "You can't have a 0 for the numerator! Error code # 1 "
}
else
return "You can't divide by zero! Error code number 0, restarting the calculator..."; // my error message
}
यह स्पष्ट रूप से काम नहीं होगा, लेकिन वहां यह तो मैं प्रत्येक त्रुटि संदेश के लिए एक अलग समारोह लिख नहीं कर रहा हूँ बनाने के लिए एक तरीका है?
एक शुरुआत के लिए की तरह लंबाई त्रुटि के लिए अपने स्वयं के अपवाद वर्ग बना सकते हैं कानूनी नहीं है। – jogojapan
'नोज़रो' को 'testException' के अंदर कोई सदस्य नहीं होना चाहिए (या नहीं होना चाहिए)। इसे बाहर ले जाएं! – SuperSaiyan
विचार आमतौर पर है कि आप प्रत्येक प्रकार के अपवाद के लिए एक अलग वर्ग लागू करते हैं। उनमें से प्रत्येक 'what()' फ़ंक्शन को अलग-अलग तरीके से ओवरराइड करता है, और आप स्ट्रिंग में टाइप-संबंधित जानकारी को शामिल करने के लिए स्पष्ट रूप से स्वतंत्र हैं। – jogojapan