異常–提醒自己

來源:互聯網
上載者:User

今天寫一個程式突然發現一個問題,我拋出的異常沒有被截獲直接記憶體泄露死掉了。找了半天發現問題的所在。記下來,提醒自己。

 

try

{

       …

       throw std:: out_of_range(“出界了”);

       …

}

catch(std::exception const& ex)

{

       std::cerr << "Exception: " << ex.what() << std::endl;

       return EXIT_FAILURE;

}

這是我的本意,要寫的代碼,但是由於我使用了VA協助工具輔助,在敲完::e之後直接確定卻出來了bad_exception,沒注意就過去了,結果調試在throw這裡就記憶體泄露了。

現在我將代碼修改為

try

{

       …

       throw std:: out_of_range(“出界了”);

       …

}

catch(std:: out_of_range const& ex)

{

       std::cerr << "Exception: " << ex.what() << std::endl;

       return EXIT_FAILURE;

}

catch(std::exception const& ex)

{

       std::cerr << "Exception: " << ex.what() << std::endl;

       return EXIT_FAILURE;

}

 

在以後的代碼中一定要記住使用std::exception來結尾,以保證拋出的異常都能夠被截獲。

或者使用

catch(…)

{

       std::cerr << "Exception is caught!" << std::endl;

       return EXIT_FAILURE;

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.