Answer by bernie for Using throw to replace return in C++ non-void functions
I think it is the wrong question asking if return can be replaced by throw. return and throw are doing two very different things:return is used to return regular results of a functions.throw is the...
View ArticleAnswer by Robert Andrzejuk for Using throw to replace return in C++ non-void...
Just because the function is throwing as the last call, doesn't mean it is replacing return. It is just the flow of logic. The question shouldn't be :is it a good practice to replace return with...
View ArticleAnswer by displayName for Using throw to replace return in C++ non-void...
Your question is language agnostic.Return and Throw have different purposes. Return is for returning a value; while,Throw is for throwing an exception; and,An exception should be thrown in truly...
View ArticleAnswer by Brian Bi for Using throw to replace return in C++ non-void functions
A function should throw an exception when it is unable to meet its postcondition. (Some functions may also throw exceptions when their preconditions are not met; that's a different topic that I won't...
View ArticleAnswer by Abion47 for Using throw to replace return in C++ non-void functions
No, throw is not a good semantic replacement for return. You only want to use throw when your code has done something that it should not be doing, not to signify the perfectly valid negative result of...
View ArticleAnswer by roschach for Using throw to replace return in C++ non-void functions
The concepts of this answer are taken from the C++ Programming language by Bjarne Stroustrup.SHORT ANSWERYes, exception-throwing can be used as returning value method. An example is the following for a...
View ArticleAnswer by Pedro LM for Using throw to replace return in C++ non-void functions
In addition to the other answers, there's also performance: Catching an exception incurs a run-time overhead (see this answer) compared to an if clause.(Of course, we're talking about microseconds......
View ArticleAnswer by armitus for Using throw to replace return in C++ non-void functions
What you have mentioned is not good programming practice. Replacing a return statement with throw is not acceptable in production-level code, especially with automated testing platforms that generate...
View ArticleAnswer by eerorika for Using throw to replace return in C++ non-void functions
In C++ functions, is it a good practice to replace return with throw?Return is not something that can be replaced by a throw in general.In exceptional cases where you have nothing to return, throwing...
View ArticleAnswer by Code-Apprentice for Using throw to replace return in C++ non-void...
return and throw have two different purposes and should not be considered interchangeable. Use return when you have a valid result to send back to the caller. On the other hand, use throw when some...
View ArticleUsing throw to replace return in C++ non-void functions
In C++ functions, is it a good practice to replace return with throw? For example, I have the following code// return indices of two numbers whose sum is equal to targetvector<int>...
View Article
More Pages to Explore .....