Quantcast
Channel: Using throw to replace return in C++ non-void functions - Stack Overflow
Browsing all 11 articles
Browse latest View live

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 Article



Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article


Answer 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 Article

Using 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
Browsing all 11 articles
Browse latest View live




Latest Images