Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

throwing exception for a generic reason c++

Tags:

c++

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
like image 273
cybertextron Avatar asked Jul 24 '12 16:07

cybertextron


People also ask

Can you throw exceptions in C?

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.

Should I throw generic exception?

Java static code analysis: Generic exceptions should never be thrown.

What is throwing an exception in C Plus Plus?

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.

What is the point of throwing an exception?

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.


2 Answers

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");
like image 114
PermanentGuest Avatar answered Sep 28 '22 11:09

PermanentGuest


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.

like image 34
Flexo Avatar answered Sep 28 '22 10:09

Flexo