如何退出功能?什么是程序计数器的try-catch-throw [英] How to exit out of a function ? what is try-catch-throw in terms of Program Counter

查看:61
本文介绍了如何退出功能?什么是程序计数器的try-catch-throw的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些这样的代码:


(如果(测试)

(退出)

(做点什么) )




(如果(测试)

(做某事)

(退出) )

各种级别的嵌套。


我有几个问题,基本到复杂。


(1)什么是(退出)的lisp等价成语,如bash或

in C.

(2)处理此类问题的最佳做法是什么? />

(3)处理这种

问题的中间做法是什么。


注意:我真的害怕尝试抓住。我从来没有能够理解它,因为它不存在于C中我无法使用
真的可视化C语言中的构造。这就是我的

大脑可以处理。如果你理解得那么好,你可以告诉我如何在

C中真正实现这种构造,然后通过扩展我可以看到那种程序流程
LISP中的
。无论是命令式编程还是功能性,

下面都有程序计数器和汇编。 C很接近

来装机,以至于几乎是装配。所以在C中理解try-c-

t相当于理解

机器语言的水平。


因此我需要在C和C ++小组中自由转交。

I have some code like this:

(if (test)
(exit)
(do something))
or

(if (test)
( do something)
(exit))
Various levels of nestings.

I have several questions, basic to sophisticated.

(1) What is the lisp equivalent idiom for (exit) as in bash or
in C.
(2) What is the best practice to handle this kind of problems?

(3) What is the intermediate practice to handle this kind of
problems.

NOTE: I am really afraid of try-catch-throw. I have never been
able to understand it since it does not exist in C and I cant
really visualize the construct in terms of C. That is what my
brain can process. If you understand it so well, you can show
me how one would really implement that kind of construct in
C and then by extension I can see that kind of program flow
in LISP. Whether its imperative programming or functional,
beneath there is program counter and assembly. C is close
to machine so much that it is almost assembly. So understanding try-c-
t in C is equivalent to understanding at
the level of machine language.

I therefore take the liberty to crosspost in C and C++ groups.

推荐答案

10月20日下午1:45,gnuist ... @ gmail .com写道:
On Oct 20, 1:45 pm, gnuist...@gmail.com wrote:

我有一些这样的代码:


(if(test)

(退出)

(做点什么))





(if(test)

(做点什么)

(退出))


各种级别的嵌套。


我有几个问题,基本到复杂。


(1)什么是(退出)的lisp等价习语,如bash或

in C.

(2)处理这类问题的最佳做法是什么?


(3)处理这种问题的中间做法是什么? $>
问题。


注意:我真的害怕尝试 - 赶上掷。我从来没有能够理解它,因为它不存在于C中我无法使用
真的可视化C语言中的构造。这就是我的

大脑可以处理。如果你理解得那么好,你可以告诉我如何在

C中真正实现这种构造,然后通过扩展我可以看到那种程序流程
LISP中的
。无论是命令式编程还是功能性,

下面都有程序计数器和汇编。 C很接近

来装机,以至于几乎是装配。所以在C中理解try-c-

t相当于理解

机器语言的水平。


因此我需要C和C ++组中交叉插入的自由。
I have some code like this:

(if (test)
(exit)
(do something))

or

(if (test)
( do something)
(exit))

Various levels of nestings.

I have several questions, basic to sophisticated.

(1) What is the lisp equivalent idiom for (exit) as in bash or
in C.
(2) What is the best practice to handle this kind of problems?

(3) What is the intermediate practice to handle this kind of
problems.

NOTE: I am really afraid of try-catch-throw. I have never been
able to understand it since it does not exist in C and I cant
really visualize the construct in terms of C. That is what my
brain can process. If you understand it so well, you can show
me how one would really implement that kind of construct in
C and then by extension I can see that kind of program flow
in LISP. Whether its imperative programming or functional,
beneath there is program counter and assembly. C is close
to machine so much that it is almost assembly. So understanding try-c-
t in C is equivalent to understanding at
the level of machine language.

I therefore take the liberty to crosspost in C and C++ groups.



对于

来说,有没有关于try-catch-throw的解释?这种解释是否更简单?

Is there any explanation of try-catch-throw in terms of
gotos than PC ? Is that explanation more simpler ?


< gn ******* @ gmail.com在消息中写道

news:11 ** ********************@k35g2000prh.googlegr oups.com ...
<gn*******@gmail.comwrote in message
news:11**********************@k35g2000prh.googlegr oups.com...

>我有一些代码是这样的:


(如果(测试)

(退出)

(做点什么))





(如果(测试)

(做某事)

(退出))


各种级别的嵌套。


我有几个问题,基本到复杂。


(1)什么是(退出)的lisp等价习惯用法,如bash或

in C.
>I have some code like this:

(if (test)
(exit)
(do something))
or

(if (test)
( do something)
(exit))
Various levels of nestings.

I have several questions, basic to sophisticated.

(1) What is the lisp equivalent idiom for (exit) as in bash or
in C.



你的留言帖是询问 ;如何退出函数...但是你是b $ b显示几乎退出程序的代码。 exit()会这样做。


如果你想退出函数,那么返回正确的关键字。

如:


if(条件)

返回;

(做点什么)


if(condition)

(做点什么)

返回;


返回可以返回一个值,如果函数返回一个值,例如

返回1;

返回foo;

返回栏();

等..

Your message post is asking "How to exit out of a function..." yet you are
showing code that almost exits out of the program. exit() would do that.

If you wish to exit out of the function then the correct keyword is return.
Such as:

if (condition)
return;
(do something)

if ( condition )
(do something )
return;

return can return a value if the function returns a value, such as
return 1;
return foo;
return bar();
etc..


(2)处理这类问题的最佳做法是什么?
(2) What is the best practice to handle this kind of problems?



这取决于编码风格。有一些编码风格表明任何

函数只有一个返回点。在这种情况下你会这样做:


如果(条件)

(做点什么)

返回;


就个人而言,我选择提前退货,如果这是我认为是错误的

条件。


if(condition )

返回NULL;

(做点什么)

返回值;


确实有没有最好的方法,这取决于编码标准以及

代码编写,读取和维护的容易程度。

It depends on coding style. There is some coding style that states that any
function will only have one return point. In which case you would do:

if ( condition )
( do something )
return;

Personally, I chose to return early if it''s what I would consider an error
condition.

if ( condition )
return NULL;
( do something )
return Value;

There really is no best way, it depends on coding standard and how easy the
code is to write, read and maintain.


(3)什么是处理这种问题的中间做法。
(3) What is the intermediate practice to handle this kind of
problems.



同样,这取决于问题。在我职业生涯的不同时期,每当我想要回到
结束时,我就会有回报。

我发现,如果我的代码更难以编写,阅读

并维护,那么我会提前检查条件并提前返回,如果我可以支付b $ b。我尽量不在功能中间返回,但只在顶部

或底部。但这取决于具体情况。

Again, it depends on the problems. At different times in my career I have
gone with returning whenever I''ve wanted to to returning only at the very
end.
I find that huge if statments, however, make my code harder to write, read
and maintain, so I will check for conditions early and return early if I
can. I try not to return in the middle of a function, but only at the top
or bottom. But it depends on the situation.


注意:我真的害怕尝试捕获。我从来没有能够理解它,因为它不存在于C中我无法使用
真的可视化C语言中的构造。这就是我的

大脑可以处理。如果你理解得那么好,你可以告诉我如何在

C中真正实现这种构造,然后通过扩展我可以看到那种程序流程
LISP中的
。无论是命令式编程还是功能性,

下面都有程序计数器和汇编。 C很接近

来装机,以至于几乎是装配。所以在C中理解try-c-

t相当于理解

机器语言的水平。


因此我需要C和C ++组中交叉插入的自由。
NOTE: I am really afraid of try-catch-throw. I have never been
able to understand it since it does not exist in C and I cant
really visualize the construct in terms of C. That is what my
brain can process. If you understand it so well, you can show
me how one would really implement that kind of construct in
C and then by extension I can see that kind of program flow
in LISP. Whether its imperative programming or functional,
beneath there is program counter and assembly. C is close
to machine so much that it is almost assembly. So understanding try-c-
t in C is equivalent to understanding at
the level of machine language.

I therefore take the liberty to crosspost in C and C++ groups.



至于try ... catch块,我尝试仅保留那些仅用于实际错误。

我发现我需要的一个原因当一个函数应该使用它们时,
返回一个对象的引用,并且该对象不存在。我必须要回报一些东西,而且还不够好。所以我通过

抛弃了这个功能。


(未经测试的代码)


Player& FindPlayer(const std :: string& Name,std :: map< std :: string,Player)

{

std :: map< std :: string,Player> ; :: iterator it = Player.find(Name);

if(it == Player.end())

{

/在地图中找不到/ Player。必须归还一些东西。让我们只用

抛出。

抛出std :: exception(未找到玩家);

}

return(* it).second;

}


int main()

{

// ...

尝试

{

Player ThisPlayer& = FindPlayer(SomePlayer);

//使用ThisPlayer

}

castch(std :: exception e)

{

std :: cout<< 播放器 << SomePlayer<< "没找到。\ n" ;;

}

}

As for try...catch blocks, I try to reserve those only for actual errors.
One reason I have found I need to use them when a function is supposed to
return a reference to an object, and that object doesn''t exist. I have to
return something, and something is not good enough. So I get around it by
throwing out of the function.

(Untested code)

Player& FindPlayer( const std::string& Name, std::map<std::string, Player)
{
std::map<std::string, Player>::iterator it = Player.find( Name );
if ( it == Player.end() )
{
// Player was not found in map. Have to return something. Lets just
throw.
throw std::exception("Player not found");
}
return (*it).second;
}

int main()
{
// ...
try
{
Player ThisPlayer& = FindPlayer( SomePlayer );
// use ThisPlayer
}
castch ( std::exception e )
{
std::cout << "Player " << SomePlayer << " not found.\n";
}
}


gn ******* @ gmail.com 写道:

我有一些代码像这样:


(如果(测试)

(退出)

(做点什么))





(如果(测试)

(做点什么)

(退出))
I have some code like this:

(if (test)
(exit)
(do something))
or

(if (test)
( do something)
(exit))



这是Lisp,是吗?这样说会很好,因为我们自然会假设

发布到comp.lang.c的任何内容都是C.

That''s Lisp, yes? Saying so would be good, since we naturally assume
that anything posted to comp.lang.c is C.


各种级别的嵌套。


我有几个问题,基本到复杂。


(1)什么是(退出)的lisp等价习语,如bash或者

in C.
Various levels of nestings.

I have several questions, basic to sophisticated.

(1) What is the lisp equivalent idiom for (exit) as in bash or
in C.



你是否期望C程序员知道Lisp中的(退出)是什么?


[snip]

You''re expecting C programmers to know what (exit) does in Lisp?

[snip]


因此我冒昧地在C和C ++组中进行交叉转换。
I therefore take the liberty to crosspost in C and C++ groups.



这很少是一个好主意。


-

Keith Thompson( The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

That''s rarely a good idea.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


这篇关于如何退出功能?什么是程序计数器的try-catch-throw的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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