C++ 异常类使用递归函数失败

C++ 异常类使用递归函数失败,c++,visual-studio-2010,recursion,throw,C++,Visual Studio 2010,Recursion,Throw,我有一个名为exception的异常类,我正在调用一个递归函数,该函数在遍历映射时被调用了大约200次。RecursiveFunction()是包含参数映射(将字符串映射到类参数)的类的一部分。类param包含一个min、一个max以及min和max之间的步数,因此可以使用每个参数集运行一组函数。因此,RecursiveFunction()在给定“current”参数的情况下通过映射循环运行一组函数 bool RecursiveFunction(map<string,param>::

我有一个名为exception的异常类,我正在调用一个递归函数,该函数在遍历映射时被调用了大约200次。RecursiveFunction()是包含参数映射(将字符串映射到类参数)的类的一部分。类param包含一个min、一个max以及min和max之间的步数,因此可以使用每个参数集运行一组函数。因此,RecursiveFunction()在给定“current”参数的情况下通过映射循环运行一组函数

bool RecursiveFunction(map<string,param>::iterator current) {
   map<string,param>::iterator last = parameters.end();
   last--;
   if( current == last )
       return true;
   else {
       // Do some things
       if(something_wrong)
           throw Exception("RecursiveFunction()","Something went wrong");
       ++current;
       RecursiveFunction(current);
   }
}
奇怪的是,代码在以下两种情况下都能顺利运行:

bool RecursiveFunction(map<string,param>::iterator current) {
    ...
    if(something_wrong)
        throw "";
    ...
   }
bool递归函数(映射::迭代器当前){
...
如果(出了什么事)
抛出“;
...
}

bool递归函数(映射::迭代器当前){
...
如果(出了什么事){
Exception exc=Exception(“RecursiveFunction(),“出错”);
ThrowException(exc);//ThrowException(){throw exc;}
}
...
}
代码没有命中“抛出”,因此没有构造或复制异常(通过断点确认)。如果一个类没有在函数中得到实例化,为什么类的存在会影响函数的结果

编辑:

我能够使用一个完整的示例(使用Visual Studio 2010)重现这一点:

#包括
使用名称空间std;
类异常:public std::Exception{
私人:
char gsCallPath[1001];
字符gsError[1001];
公众:
异常(const char*sErrorCallPath,const char*strownError)
{
strcpy(gsError,strownerror);
strcpy(gsCallPath、sErrorCallPath);
}
~Exception(){
}
};
布尔递归函数(int n);
int main(){
递归函数(500);
}
布尔递归函数(int n){
不能包含
void f(){
if(false){
抛出“;
//抛出std::exception();
}
返回f();
}
int main(){
f();
}

查看程序集,函数似乎为异常对象保留堆栈空间,不管是否抛出异常对象。因此,对于
“”
,此函数保留204个字节(
sub esp,0CCh
)在
std::exception
的情况下,它是
子esp,0D4h
,即多8个字节,即
sizeof(std::exception)-sizeof(char*)

代码中不清楚
参数是什么,函数如何调用,
//做一些事情
做了什么,或者当
出错时
将是
真的
。事实上,不可能回答这个问题。它以什么方式“失败”?(最有可能的是,递归导致堆栈溢出;幸运的是,将其改为使用迭代应该很简单,除非您的实际代码与您发布的代码非常不同)。参数是函数的输入。如果某些参数组合不正确,例如参数[“总体输出最小年龄”]<参数[“开始年份”]这是不相关的,因为有些错误是不正确的。这个函数已经正常工作。异常类的加入使它失败。我想知道当C++编译时,是否有一些改变函数的组成。我不关心递归函数。@65否,
当前
是该函数的输入。符号
参数
是什么?它来自何处?请。“它以前工作过,一些无害的更改使它断裂”通常意味着您在某个时候调用了未定义的行为。谢谢。我添加了一个示例,证明我不能在递归函数中使用Exception类,因为无论是否调用它,它都会被保留。使用ThroweException函数似乎是最好的解决方案。@user1582665不知道最好的方法。您是你确信你的递归有一天不会更深入吗?我想你应该想出一个迭代算法。“最好”是一个相对的说法。我会重新考虑递归。再次感谢。
bool RecursiveFunction(map<string,param>::iterator current) {
    ...
    if(something_wrong)
        throw "";
    ...
   }
bool RecursiveFunction(map<string,param>::iterator current) {
    ...
    if(something_wrong) {
        Exception exc = Exception("RecursiveFunction()","Something went wrong");
        ThrowException(exc); //ThrowException() { throw exc; }
    }
    ...
   }
#include <iostream>

using namespace std;

class Exception: public std::exception {
private:
    char        gsCallPath[1001];
    char        gsError[1001];
public:
    Exception(const char* sErrorCallPath, const char* sThrownError)
    {
       strcpy(gsError,sThrownError);
       strcpy(gsCallPath,sErrorCallPath);
    }
    ~Exception() {
    }
};

bool RecursiveFunction(int n);

int main() {
    RecursiveFunction(500);
}

bool RecursiveFunction(int n) {

    cout << n << '\n';

    if (n == 0)
        return true;
    else {
        if(false) {
            throw Exception("","");
            //throw "";
        }
        else
        {
            RecursiveFunction( n-1 );
        }
    }
}
#include <exception>
void f() {
    if (false) {
        throw "";
        //throw std::exception();
    }
    return f();
}
int main() {
    f();
}