2015-11-07 67 views
0

我有一個例外,在此代碼爲什麼我在VS2015中創建正則表達式對象時有異常?

Date & Date::operator=(const std::string & str){ 
    if (str.size() != 10) 
     throw std::exception("not a date"); 


    std::regex r;// exception here 
    try { // exception here 
     std::regex regEx ("^[0-9]{4}[0-9]{2}[0-9]{2}"); 
    } 
    catch (std::exception &e) { 
     e.what(); 
    } 
    std::regex delims("([^.,;-]+)"); 

    std::smatch match; 
    if (std::regex_match(str.cbegin(), str.cend(), match, regEx)) { 
     std::stringstream ss; 
     std::string tmp(match.str()); 
     std::copy(std::sregex_token_iterator(tmp.cbegin(), tmp.cend(), delims, -1), 
     std::sregex_token_iterator(), 
     std::ostream_iterator<std::string>(ss, "\n")); 
     ss >> year; 
     ss >> month; 
     ss >> day; 
    } 
    return *this; 
} 

如果我的作品把類似的代碼在主要功能normaly。 我使用Visual Studio 2015社區版。

+0

什麼是例外? – hjpotter92

+0

Test.exe中0x77383E28有一個未處理的異常:Microsoft C++的異常:std ::內存地址爲0x00C7FAF4的異常 – Jek

+0

我無法捕捉到它。 – Jek

回答

1

構建正則表達式時不會發生您的異常,但是當您拋出自己的異常時。

首先你要檢查你的字符串正好是10個字符長。 如果你把尺寸10一串字符串中的一切工作,因爲它應該:

int main() 
{ 
    std::string str = "20151107AB"; 
    int year = 0; 
    int month = 0; 
    int day= 0; 

    if (str.size() != 10) 
     throw std::exception("not a date"); 

    std::regex regEx("^[0-9]{4}[0-9]{2}[0-9]{2}"); 
    std::regex delims("([^.,;-]+)"); 

    std::smatch match; 
    if (std::regex_match(str.cbegin(), str.cend(), match, regEx)) { 
     std::stringstream ss; 
     std::string tmp(match.str()); 
     std::copy(std::sregex_token_iterator(tmp.cbegin(), tmp.cend(), delims, -1), 
      std::sregex_token_iterator(), 
      std::ostream_iterator<std::string>(ss, "\n")); 
     ss >> year; 
     ss >> month; 
     ss >> day; 
    } 
} 

如果刪除了「AB」作爲您的問題描述,你會得到錯誤。

int main() 
{ 
    std::string str = "20151107"; 
    int year = 0; 
    int month = 0; 
    int day= 0; 

    if (str.size() != 10) 
     throw std::exception("not a date"); 

    std::regex regEx("^[0-9]{4}[0-9]{2}[0-9]{2}"); 
    std::regex delims("([^.,;-]+)"); 

    std::smatch match; 
    if (std::regex_match(str.cbegin(), str.cend(), match, regEx)) { 
     std::stringstream ss; 
     std::string tmp(match.str()); 
     std::copy(std::sregex_token_iterator(tmp.cbegin(), tmp.cend(), delims, -1), 
      std::sregex_token_iterator(), 
      std::ostream_iterator<std::string>(ss, "\n")); 
     ss >> year; 
     ss >> month; 
     ss >> day; 
    } 
} 

如果您現在刪除長度檢查一切正常,因爲它應該再次。

int main() 
{ 
    std::string str = "20151107"; 
    int year = 0; 
    int month = 0; 
    int day= 0; 

    std::regex regEx("^[0-9]{4}[0-9]{2}[0-9]{2}"); 
    std::regex delims("([^.,;-]+)"); 

    std::smatch match; 
    if (std::regex_match(str.cbegin(), str.cend(), match, regEx)) { 
     std::stringstream ss; 
     std::string tmp(match.str()); 
     std::copy(std::sregex_token_iterator(tmp.cbegin(), tmp.cend(), delims, -1), 
      std::sregex_token_iterator(), 
      std::ostream_iterator<std::string>(ss, "\n")); 
     ss >> year; 
     ss >> month; 
     ss >> day; 
    } 
} 

另外,如果趕上例外一切仍是罰款(但不執行代碼)

int main() 
{ 
    try 
    { 
     std::string str = "20151107"; 
     int year = 0; 
     int month = 0; 
     int day = 0; 

     if (str.size() != 10) 
      throw std::exception("not a date"); 

     std::regex regEx("^[0-9]{4}[0-9]{2}[0-9]{2}"); 
     std::regex delims("([^.,;-]+)"); 

     std::smatch match; 
     if (std::regex_match(str.cbegin(), str.cend(), match, regEx)) { 
      std::stringstream ss; 
      std::string tmp(match.str()); 
      std::copy(std::sregex_token_iterator(tmp.cbegin(), tmp.cend(), delims, -1), 
       std::sregex_token_iterator(), 
       std::ostream_iterator<std::string>(ss, "\n")); 
      ss >> year; 
      ss >> month; 
      ss >> day; 
     } 
    } 
    catch (std::exception& e) 
    { 
     //handle 
    } 
} 

所以我對這個假設是,你不捕獲異常的任何地方,這可能導致內存損壞,因爲標準沒有定義在這種情況下堆棧是否必須展開。

我會推薦給 1.閱讀Does it make sense to catch exceptions in the main(...)? 2.正確捕獲由Date::operator=

拋出的異常也許你還需要安裝一個全球性的異常處理程序(安全地關閉您的程序)。

還有一件事:當你只需要8個字符時,爲什麼你檢查日期字符串的長度正好是10?

+0

謝謝!現在它工作正常。 – Jek

相關問題