2012-11-14 46 views
1

我試圖查看無效索引處的元素來測試我的錯誤捕獲,當我嘗試調試它時遇到無源代碼或嘗試運行時出現Unhanded Exception Error它通常爲:沒有來源可用throw throw

#include <iostream> 
#include "MyArray.h" 

using namespace std; 

int main() 
{ 
    MyArray<int> arr1; 
    MyArray<int> arr2(2); 

    arr2.Add(11); 
    arr2.Add(22); 
    arr2.Add(33); 

    arr2.At(5); // Test to check error 

    system("pause"); 
    return 0; 
} 

arr2.At(5);調用這個函數在我MyArray.h文件,在那裏我得到我的錯誤:

// MyArray.h 
template <class elemType> 
elemType & MyArray<elemType>::At(int index) 
{ 
    if (index < 0 || index >= _size) 
     throw "elemType & MyArray<elemType>::At(int index) Index out of range!"; // Where the problem is 

    return list[index]; 
} 

錯誤,我得到的,當我正常運行:

Unhandled exception at 0x763ec41f in MyArrayProject.exe: Microsoft C++ exception: char at memory location 0x0032fa60.. 

錯誤,我得到的,當我試圖在arr2.At(5);調試它和它擊中throw "elemType & MyArray<elemType>::At(int index) Index out of range!";

No Source Available 
There is no source code available for the current location. 

Call stack location: 
    msvrc100d.dll!_CxxThrowException(void * pExceptionObject, const_s__ThrowInfo) Line 91 

以前沒有經歷過這種錯誤,不知道應該如何解決這個問題,任何幫助將不勝感激! :)

回答

1

無源可用只是調試器/ IDE告訴你,在哪裏發生異常(或不是你的情況),它沒有源代碼來顯示你的當前行執行是。

在你的例子中,你沒有捕獲它,所以它會在自動生成代碼的深處(調用main())。

在At(5)通話周圍試一試,它會在那裏抓住它(假設你在那裏放置一個斷點)。

+0

我忽略了!謝謝! –