你如何“打破”功能?(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 } }更多推荐
发布评论