你如何“打破”功能?(How do you “break” out of a function?)

系统教程 行业动态 更新时间:2024-06-14 17:01:34
你如何“打破”功能?(How do you “break” out of a function?)

给定一个函数返回一个值,是否有可能在给定的条件下退出函数而不返回任何东西? 如果是这样,你怎么能做到这一点?

例:

int getNumber () { . . . }

所以说你在这个功能。 有没有办法退出它的方法?

Given a function that returns a value, is it possible to exit the function given a certain condition without returning anything? If so, how can you accomplish this?

Example:

int getNumber () { . . . }

So say you are in this function. Is there a way to exit it without it doing anything?

最满意答案

你有两个选择: return或throw 。

int getNumber() { return 3; } int getNumber() { throw string("Some Var"); }

如果你throw ,你必须catch你扔的类型。

int maint(int argc, char ** argc) { try { getNumber(); } catch(string std) { //Your code will execute here if you throw } }

You have two options: return something or throw.

int getNumber() { return 3; } int getNumber() { throw string("Some Var"); }

If you throw, you have to catch the type you threw.

int maint(int argc, char ** argc) { try { getNumber(); } catch(string std) { //Your code will execute here if you throw } }

更多推荐

本文发布于:2023-04-20 18:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/3378aa2cd5d61010fd48cc70dc4617b9.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:功能   break   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!