在C++中抛出异常异常

问题描述:

此代码有效;在C++中抛出异常异常

int at(int index) { 
    if(index < 1 || index >= size) 
    throw 0; 

    return x[index]; 
} 

但这并不

int at(int index) { 
    if(index < 1 || index >= size) 
    throw std::out_of_range; 

    return x[index]; 
} 

我得到的错误 “之前预计主要表达 ';'”。现在...我很惊讶,因为我知道的std :: out_of_range存在,我有

#include <stdexcept> 
+2

这两个函数有什么区别? – kennytm 2010-04-25 19:58:51

+0

oops,复制粘贴错误,现在没关系。 – Suugaku 2010-04-25 20:00:22

+6

为什么是0越界? – 2010-04-25 22:23:28

更换throw std::out_of_range;throw std::out_of_range ("blah");。即你需要创建一个对象,你不能抛出一个类型。