,我有以下我的构造函数的代码块(这只是一个例子,这个问题是不是split
,而约抛出一般异常。此外,升压库不能使用。抛出异常的通用理由C++
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.
}
}
我想是抛出一个异常如果目录不存在,它不能被创建。我想抛出一个通用的异常,我怎么能做到这一点在C++
? PS:dest
has th Ë格式如下:
dest = /usr/var/temp/current/tree
'令牌= strtok的((字符*)DEST做到这一点.c_str(),PATH_SEPARATOR)//'ouch – chris 2012-07-24 16:30:01
@chris你有更好的建议吗? – cybertextron 2012-07-24 16:31:17
Boost文件系统有一个'create_directories',它为你免费提供所有这些,并且具有很好的错误语义。 – Flexo 2012-07-24 16:33:17