MFC 异常中断

李水云 2018-11-07 09:56:52
void main()
{
if(a()) return;
if(b()) return;
if(c()) return;
}

BOOL a()
{
if(1<2) return true;
//能不能在这里直接退出当前程序。
}


在MFC建了个用户界面,点击按钮后运行一段程序。在主程序中会调用其它函数,根据函数的结果判断是否继续进行。能不能再判断函数中直接结束当前程序,而不是返回主程序中再判断。
因为是用MFC建立界面,只想退出当前按钮操作二不退出界面,故不能用exit()。有没有其他更好的方法,不用逐级判断返回值。
...全文
160 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ForgetTomorrow 2018-11-08
  • 打赏
  • 举报
回复
再怎么弄都不能,函数调用堆栈都会要回收的
赵4老师 2018-11-07
  • 打赏
  • 举报
回复
exit(1);
zgl7903 2018-11-07
  • 打赏
  • 举报
回复
可以抛出异常


void DoSomeOperation( )
{
// Processing
// If something goes wrong...
AfxMessageBox( "The x operation failed" );
AfxThrowUserException( );
}

BOOL TrySomething( )
{
TRY
{
// Could throw a CUserException or other exception.
DoSomeOperation( );
}
CATCH( CUserException, e )
{
return FALSE; // User already notified.
}
AND_CATCH( CException, e )
{
// For other exception types, notify user here.
AfxMessageBox( "Some operation failed" );
return FALSE;
}
END_CATCH
return TRUE; // No exception thrown.
}


cctrys 2018-11-07
  • 打赏
  • 举报
回复
你这个需求有点奇葩,本身函数都是以堆栈的方式一层一层来调用的,A->B->C->D,直接在上层的D函数中想让A或者B的堆栈退出,这个恐怕不太可能吧!
赵4老师 2018-11-07
  • 打赏
  • 举报
回复
仅供参考:
#include <iostream>
using namespace std;
struct MyStruct {
int Nodedata;
MyStruct *pLeft;
MyStruct *pRight;
} s1,s2,s3,s4,s5,s6,s7,s8;
void ashow(MyStruct * p) {
if (p != nullptr) {
ashow(p->pLeft);
ashow(p->pRight);
cout << p->Nodedata << endl;
if (p->Nodedata == 4) {
throw 1;//退出递归.直接返回.
}
}
}
int main() {
MyStruct *pRoot;//根

pRoot = &s1;
s1.Nodedata = 1;
s2.Nodedata = 2;
s3.Nodedata = 3;
s4.Nodedata = 4;
s5.Nodedata = 5;
s6.Nodedata = 6;
s7.Nodedata = 7;
s8.Nodedata = 8;

s1.pLeft = &s2;
s1.pRight = &s3;

s2.pLeft = &s4;
s2.pRight = &s5;

s3.pLeft = &s6;
s3.pRight = &s7;
try {
ashow(pRoot);
} catch (int err) {
cout<<"catch err "<<err<<endl;
}
return 0;
}
//4
//catch err 1
//
李水云 2018-11-07
  • 打赏
  • 举报
回复
引用 7 楼 zgl7903 的回复:



BOOL a()
{
……
// If something goes wrong...
AfxMessageBox( "The x operation failed" );
AfxThrowUserException( );
}


BOOL mainFun()
{
TRY
{
// Could throw a CUserException or other exception.
a( ); //a抛出异常时 程序直接到 CATCH( CUserException, e )
b();
c();
}
CATCH( CUserException, e )
{
return FALSE; // User already notified.
}
AND_CATCH( CException, e )
{
// For other exception types, notify user here.
AfxMessageBox( "Some operation failed" );
return FALSE;
}
END_CATCH
return TRUE; // No exception thrown.
}




意思是不管函数体中有几级调用,只要函数体调用的任意函数中抛出异常,就会立即返回主函数体中处理异常。是这个意思吧?


zgl7903 2018-11-07
  • 打赏
  • 举报
回复



BOOL a()
{
……
// If something goes wrong...
AfxMessageBox( "The x operation failed" );
AfxThrowUserException( );
}

BOOL mainFun()
{
TRY
{
// Could throw a CUserException or other exception.
a( ); //a抛出异常时 程序直接到 CATCH( CUserException, e )
b();
c();
}
CATCH( CUserException, e )
{
return FALSE; // User already notified.
}
AND_CATCH( CException, e )
{
// For other exception types, notify user here.
AfxMessageBox( "Some operation failed" );
return FALSE;
}
END_CATCH
return TRUE; // No exception thrown.
}
李水云 2018-11-07
  • 打赏
  • 举报
回复
引用 1 楼 net_syc 的回复:
你这个需求有点奇葩,本身函数都是以堆栈的方式一层一层来调用的,A->B->C->D,直接在上层的D函数中想让A或者B的堆栈退出,这个恐怕不太可能吧!


不是有exit()和assret()这种类型的函数,看看还有没有其他更合适的。
李水云 2018-11-07
  • 打赏
  • 举报
回复
引用 2 楼 zgl7903 的回复:
可以抛出异常


void DoSomeOperation( )
{
// Processing
// If something goes wrong...
AfxMessageBox( "The x operation failed" );
AfxThrowUserException( );
}

BOOL TrySomething( )
{
TRY
{
// Could throw a CUserException or other exception.
DoSomeOperation( );
}
CATCH( CUserException, e )
{
return FALSE; // User already notified.
}
AND_CATCH( CException, e )
{
// For other exception types, notify user here.
AfxMessageBox( "Some operation failed" );
return FALSE;
}
END_CATCH
return TRUE; // No exception thrown.
}





主要是比较懒,想在异常时直接跳出操作,不用逐级判断返回值。
李水云 2018-11-07
  • 打赏
  • 举报
回复
主要是基于MFC运行,有一个界面,用exit()和assret()都会退出界面。我所了解的只有goto loop能实现我的要求:不再执行异常之后的所有操作。

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧