,我有以下我的構造函數的代碼塊(這只是一個例子,這個問題是不是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