다음은 예외에 몇 가지 정보를 추가하고 싶을 때 자주하는 작업의 예입니다.
std::stringstream errMsg;
errMsg << "Could not load config file '" << configfile << "'";
throw std::exception(errMsg.str().c_str());
더 좋은 방법이 있습니까?
std::string소요 암시 적 생성자가 const char*...
std::exception의 자식 클래스의 비하인드 스토리 구현의 일부인 것으로 보이며 std::runtime_error및 std::logic_error. 그렇다 표준에 의해 정의 된 것과의 MSVS '버전은 <exception>또한 두 개 더 생성자, 하나 개의 복용 포함 (const char * const &)하고 다른 복용 (const char * const &, int). 개인 변수를 설정하는 데 사용됩니다 const char * _Mywhat. 만약 _Mywhat != nullptr다음, what()그것을 반환 기본값. 그것에 의존하는 코드는 아마도 이식성이 없을 것입니다.
std∷exception가있는 생성자가 없습니다char*.