I have the following chunk of code in my constructor (This is just an example, the question isn't about split
, rather about throwing a generic exception. Also, Boost library can't be used.
Transfer::Transfer(const string &dest){
try{
struct stat st;
char * token;
std::string path(PATH_SEPARATOR) // if it is \ or / this macro will solve it
token = strtok((char*)dest.c_str(), PATH_SEPARATOR) //
while(token != NULL){
path += token;
if(stat(path.c_str(), &st) != 0){
if(mkdir(path.c_str()) != 0){
std:string msg("Error creating the directory\n");
throw exception // here is where this question lies
}
}
token = strtok(NULL, PATH_SEPARATOR);
path += PATH_SEPARATOR;
}
}catch(std::exception &e){
//catch an exception which kills the program
// the program shall not continue working.
}
}
What I want is to throw an exception if the directory does not exist and it can't be created. I want to throw a generic exception, how could I do it in C++
?
PS: dest
has the following format:
dest = /usr/var/temp/current/tree
The C programming language does not support exception handling nor error handling. It is an additional feature offered by C. In spite of the absence of this feature, there are certain ways to implement error handling in C. Generally, in case of an error, most of the functions either return a null value or -1.
Java static code analysis: Generic exceptions should never be thrown.
An exception in C++ is thrown by using the throw keyword from inside the try block. The throw keyword allows the programmer to define custom exceptions. Exception handlers in C++ are declared with the catch keyword, which is placed immediately after the try block.
Exceptions are used to indicate that an error has occurred while running the program. Exception objects that describe an error are created and then thrown with the throw keyword. The runtime then searches for the most compatible exception handler.
Please check this answer. This explains how to use your own exception class
class myException: public std::runtime_error
{
public:
myException(std::string const& msg):
std::runtime_error(msg)
{}
};
void Transfer(){
try{
throw myException("Error creating the directory\n");
}catch(std::exception &e){
cout << "Exception " << e.what() << endl;
//catch an exception which kills the program
// the program shall not continue working.
}
}
Also, if you don't want your own class, you can do this simply by
throw std::runtime_error("Error creating the directory\n");
Your usage of strtok
is incorrect - it needs a char*
because it modifies the string, but it is not permitted to modify the results of a .c_str()
on a std::string
. The need to use a C style cast (which here is performing like a const_cast
) is a big warning.
You can neatly sidestep this and the path separator portability stuff by using boost filesystem, which is likely to appear in TR2 whenever that's release. For example:
#include <iostream>
#include <boost/filesystem.hpp>
int main() {
boost::filesystem::path path ("/tmp/foo/bar/test");
try {
boost::filesystem::create_directories(path);
}
catch (const boost::filesystem::filesystem_error& ex) {
std::cout << "Ooops\n";
}
}
Splits the path on the platform's separator, makes the directories if needed or throws an exception if it fails.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With