2016-09-25 128 views
0

我是編程概念的新手,並且第一次使用Visual Studio等軟件。所以現在我正在學習C++語言。 當我試圖在C++中處理error()函數時,它給了我一個錯誤消息,說「ConsoleApplication3.exe中0x76DE3E28的未處理異常:Microsoft C++異常:內存位置0x0116F8CC處的std :: runtime_error。」。這裏是我的代碼示例:'error()'C++中的函數在Visual Studio 2015中不起作用

#include "stdafx.h" 
#include <iostream> 
#include <stdexcept> 
#include <string> 
#include <vector> 
#include <algorithm> 
#include "std_lib_facilities.h" 

using namespace std; 

int area(int w , int l) 
{ 
if (w <= 0 || l <= 0) error("There's something went wrong!"); 
    return w/l; 
} 
int main() 
{ 
    int x = 3; 
    int y = 0; 
    cout << area(x, y) << endl; 
    keep_window_open(); 
} 

我已經檢查了很多次,沒有發現代碼有問題。有沒有什麼我做了錯誤的代碼?請幫助我這個傢伙。非常感謝!

+1

'error()'定義在哪裏? – tadman

+1

打賭的功能就是這樣,拋出一個異常突然結束程序的執行。 – iksemyonov

+1

它不工作,因爲''有什麼地方出錯!''在語法上是不正確的。這一點以及沒有[error()](http://man7.org/linux/man-pages/man3/error.3.html)這樣的事實 - 至少不像你使用的那樣。也看這裏:http://www.cplusplus.com/forum/unices/79591/ – paulsm4

回答

1

這是Bjarne Stroustrup的標題之一。如果您查看異常發生的位置,您將看到拋出異常。你看到的是預期的。你不應該對你剛剛得到的代碼做出假設。創建你自己的錯誤功能,並讓它做你正在尋找的東西。

相關問題