मैं एक साधारण कस्टम runtime_error करने की कोशिश कर रहा हूँ। मैं वर्ग को परिभाषित:अपनी खुद की कस्टम रनटाइम त्रुटि वर्ग कैसे करें?
#include <string>
#include <stdexcept>
namespace utils{
class FileNotFoundException: public std::runtime_error{
public:
FileNotFoundException():runtime_error("File not found"){}
FileNotFoundException(std::string msg):runtime_error(msg.c_str()){}
};
};
तो मैं त्रुटि फेंक:
bool checkFileExistence(std::string fileName)
{
boost::filesystem::path full_path = boost::filesystem::system_complete(boost::filesystem::path(fileName));
if (!boost::filesystem::exists(full_path))
{
char msg[500];
_snprintf(msg,500,"File %s doesn't exist",fileName.c_str());
throw new FileNotFoundException(msg);
}
}
और मैं एक आज़माएं/कैच ब्लॉक का उपयोग
try{
checkFileExistence(fileName);
}
catch(utils::FileNotFoundException& fnfe)
{
std::cout << fnfe.what() << std::endl;
}
रनटाइम त्रुटि को सही ढंग से FileNotFoundException लेकिन रेखा के रूप में फेंक दिया जाता है std :: cout के साथ कभी नहीं पहुंचा है और कंसोल पर कोई पंक्ति writed है।
सभी विचारों का स्वागत है। धन्यवाद!
आप अपने अपवाद वर्ग में ओवरराइड करने के बजाय डिफ़ॉल्ट तर्क का उपयोग करना चाह सकते हैं। –