【发布时间】:2011-06-21 01:14:14
【问题描述】:
我调试了我的应用程序,代码在这段代码中的 throw 语句立即崩溃:
try
{
char newbuff[8];
if(strlen(cstr) > sizeof(newbuff))
{
throw BUFFER_TOO_SMALL;
}
if(strlen(cstr) == 0)
{
throw NO_CONTENT;
}
strcpy(newbuff, cstr); //Yeah yeah yeah, I know, I'm just learning
ptr = newbuff;
}
catch(int errn)
{
cout << "error: ";
if(errn == BUFFER_TOO_SMALL)
{
cout << "storage buffer too small.\n";
return 0;
}
if(errn == NO_CONTENT)
{
cout << "no content inside of buffer.\n";
return 0;
}
}
因此,在调试时,它就在 throw 语句上崩溃了。有趣的是,CLI(在本例中为“cmd.exe”)显示了这条消息(它不是由我放入其中,来自编译器或操作系统):
This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.
我现在更倾向于 C++,因为我以前只是用 C 编程。如您所知,现在我正在尝试管理 C++ 使用的 try-catch 异常处理系统。
【问题讨论】:
-
您是否尝试在抛出 BUFFER_TO_SMALL 的函数的调用者中捕获异常?如果你没有捕捉到异常,程序就会崩溃。
-
是的,catch 语句在代码中,我只是没有提供。捕获跟随尝试。 Try 包含上面的代码。
-
您是否尝试过 catch (...) 以查看类型是否不匹配,因此您设置的 catch 没有捕获?
-
上面的代码应该可以工作,假设常量是
int's。 -
我不认为任何人的答案都有效;我会尝试不同的编译器,请稍等。
标签: c++ windows exception throw