需要专家意见 [英] Need experts opnion

查看:66
本文介绍了需要专家意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

在我目前的项目中,我正在考虑使用setjmp / lngjmp来处理异常

处理。我计划这样做的方式显示在下面的例子。

如果这是正确的方法。请问

这个方法有什么缺点吗?


# include< stdio.h>

#include< setjmp.h>

#include< stdlib.h>

jmp_buf g_env;

extern void fun1();

int main()

{

int ret;


ret = setjmp(g_env);

if(ret == 0)

{

printf(" ;执行预期的功能);

fun1();

退出(EXIT_SUCCESS);

}

否则

{

printf(Exception handling \ n);

退出(EXIT_FAILURE);

}


}

#include< stdio.h>

#include< setjmp.h>


extern jmp_buf g_env;


vo id fun1()

{

longjmp(g_env,1);

}

解决方案

va******@rediffmail.com 写道:< blockquote class =post_quotes>大家好,
在我目前的项目中,我正在考虑使用setjmp / lngjmp进行异常处理。我计划这样做的方式如下例所示。<请问这是否是正确的方法。这种方法有什么缺点吗?

#include< stdio.h>
#include< setjmp .h>
#include< stdlib.h>
jmp_buf g_env;
extern void fun1();
int main()
{
int ret;

ret = setjmp(g_env);
if(ret == 0)
{
printf("执行预期功能);
fun1();
退出(EXIT_SUCCESS);
}

{
printf("异常处理\ n);
Ë xit(EXIT_FAILURE);
}


#include< stdio.h>
#include< setjmp.h>

extern jmp_buf g_env;

void fun1()
{
longjmp(g_env,1);
}







从您发布的代码开始,有几个有效的

改进:


1)不要使用单个jmp_buf,而是使用它的数组。

2)定义宏以使异常处理更加明显。仅作为

的一个例子:


#define尝试if(setjmp(g_env))

#define CATCH else

#define THROW(x)longjmp(g_env,x)


制作代码


尝试

{

}

CATCH

{

}


....

THROW(1)


3)必要时不要忘记falg挥动。


亲切的问候。




tmp123写道:

1)不要使用单个jmp_buf,但是它的一个数组。




你能详细解释为什么jmp_buf应该是一个数组吗?


va******@rediffmail.com 写道:


在我目前的项目中,我正在考虑使用setjmp / lngjmp进行异常处理。我计划这样做的方式如下面的示例所示。请问这是否是正确的方法。这个方法有什么缺点吗?

***引用以减少垂直空间*** #include< stdio.h>
#include< setjmp.h>
#include< stdlib.h>
jmp_buf g_env;
extern void fun1();
int main()
{
int ret;

ret = setjmp(g_env);
if(ret == 0){
printf("执行预期的功能) ;);
fun1();
退出(EXIT_SUCCESS);
}
else {
printf(" Exception handling \ n");
退出(EXIT_FAILURE);
}

#include< stdio.h>
#include< setjmp.h>

extern jmp_buf g_env;

void fun1()
{
longjmp(g_env,1);
}



惊喜。你的第一个陈述ret = setjmp(...)

非法!以下是N869的引用:


环境限制


[#4] setjmp宏的调用只出现在

以下某个上下文之一:


- 选择的整个控制表达式或

迭代语句;


- 关系或相等运算符的一个操作数,

另一个操作数是一个整数常量表达式,

结果表达式是整个控制

选择或迭代语句的表达式;


- 一元的操作数!运算符生成的

表达式是

选择或迭代语句的整个控制表达式;或者


- 表达式声明的整个表达式

(可能转为无效)。


- -

"如果你想通过groups.google.com发布一个后续内容,请不要使用

破坏的回复链接在文章的底部。点击

" show options"在文章的顶部,然后点击

回复在文章标题的底部。 - Keith Thompson

详情请见:< http://cfaj.freeshell.org/google/>


Hi all,
In my current project I am thinking to use setjmp/lngjmp for exception
handling.The way I am planing to do this is shown in the below example.
Please if this is the right way to do.Are there any disadvantages in
this method?

#include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>
jmp_buf g_env;
extern void fun1();
int main()
{
int ret;

ret = setjmp(g_env);
if (ret == 0)
{
printf("Perform the intended functionality");
fun1();
exit(EXIT_SUCCESS);
}
else
{
printf("Exception handling\n");
exit(EXIT_FAILURE);
}

}
#include <stdio.h>
#include <setjmp.h>

extern jmp_buf g_env;

void fun1()
{
longjmp(g_env,1);
}

解决方案

va******@rediffmail.com wrote:

Hi all,
In my current project I am thinking to use setjmp/lngjmp for exception
handling.The way I am planing to do this is shown in the below example.
Please if this is the right way to do.Are there any disadvantages in
this method?

#include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>
jmp_buf g_env;
extern void fun1();
int main()
{
int ret;

ret = setjmp(g_env);
if (ret == 0)
{
printf("Perform the intended functionality");
fun1();
exit(EXIT_SUCCESS);
}
else
{
printf("Exception handling\n");
exit(EXIT_FAILURE);
}

}
#include <stdio.h>
#include <setjmp.h>

extern jmp_buf g_env;

void fun1()
{
longjmp(g_env,1);
}



Hi,

Starting with the code you has post, there are several posible
improvements:

1) Do not use a single jmp_buf, but an array of it.
2) Define macros to made more visible the exception handling. Only as
an example:

#define TRY if (setjmp(g_env))
#define CATCH else
#define THROW(x) longjmp(g_env,x)

mades the code

TRY
{
}
CATCH
{
}

....
THROW(1)

3) Do not forget the falg volatile when necessary.

Kind regards.



tmp123 wrote:

1) Do not use a single jmp_buf, but an array of it.



Could you please explain in detail why jmp_buf should be an array?


va******@rediffmail.com wrote:


In my current project I am thinking to use setjmp/lngjmp for
exception handling.The way I am planing to do this is shown in
the below example. Please if this is the right way to do.Are
there any disadvantages in this method?
*** quote munged to reduce vertical space *** #include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>
jmp_buf g_env;
extern void fun1();
int main()
{
int ret;

ret = setjmp(g_env);
if (ret == 0) {
printf("Perform the intended functionality");
fun1();
exit(EXIT_SUCCESS);
}
else {
printf("Exception handling\n");
exit(EXIT_FAILURE);
}
}

#include <stdio.h>
#include <setjmp.h>

extern jmp_buf g_env;

void fun1()
{
longjmp(g_env,1);
}



Surprise. Your very first statement "ret = setjmp(...)" is
illegal! The following is a quote from N869:

Environmental limits

[#4] An invocation of the setjmp macro shall appear only in
one of the following contexts:

-- the entire controlling expression of a selection or
iteration statement;

-- one operand of a relational or equality operator with
the other operand an integer constant expression, with
the resulting expression being the entire controlling
expression of a selection or iteration statement;

-- the operand of a unary ! operator with the resulting
expression being the entire controlling expression of a
selection or iteration statement; or

-- the entire expression of an expression statement
(possibly cast to void).

--
"If you want to post a followup via groups.google.com, don''t use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>


这篇关于需要专家意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆