Sto provando a eseguire un runtime_error personalizzato semplice. Io definisco la classe:Come eseguire la mia classe di errore runtime personalizzata?
#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()){}
};
};
Poi butto l'errore:
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);
}
}
e io uso un blocco try/catch
try{
checkFileExistence(fileName);
}
catch(utils::FileNotFoundException& fnfe)
{
std::cout << fnfe.what() << std::endl;
}
Errore di runtime sia correttamente gettato come FileNotFoundException ma la linea con std :: cout non viene mai raggiunto e nessuna riga viene scritta sulla console.
Tutte le idee sono benvenute. Grazie!
È possibile utilizzare un argomento predefinito anziché eseguire l'override della classe di eccezioni. –