我通过了一个长期运行的遗留ruby程序,它有很多次出现
begin
#dosomething
rescue Exception => e
#halt the exception's progress
end
Run Code Online (Sandbox Code Playgroud)
贯穿始终.
如果不追踪每一个可能的异常,每个异常都可以处理(至少不是立即),我仍然希望能够有时关闭它CtrlC.
我想以一种只添加代码的方式这样做(所以我不会影响现有的行为,或者在运行过程中错过一个被捕获的异常.)
[ CtrlC是SIGINT,或SystemExit,它似乎与SignalException.new("INT")Ruby的异常处理系统相同.class SignalException < Exception,这就是为什么会出现这个问题的原因.
我想写的代码是:
begin
#dosomething
rescue SignalException => e
raise e
rescue Exception => e
#halt the exception's progress
end
Run Code Online (Sandbox Code Playgroud)
编辑:此代码有效,只要您获得要捕获正确的异常类.这是SystemExit,Interrupt或IRB :: Abort,如下所示.
我想知道如何以及何时可以exit()像我书中的程序一样使用函数:
#include<stdio.h>
void main()
{
int goals;
printf("enter number of goals scored");
scanf("%d",&goals);
if(goals<=5)
goto sos;
else
{
printf("hehe");
exit( );
}
sos:
printf("to err is human");
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,它显示ERROR:调用未定义的函数exit().
另外,我想知道如何创建一个选项来关闭程序运行的窗口?例如,我制作了一个菜单驱动的程序,它有几个选项,其中一个是"退出菜单".如何退出程序(即关闭窗口)?