Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How do exceptions simplify my function return type and parameter types?

0
Posted

How do exceptions simplify my function return type and parameter types?

0

When you use return codes, you often need two or more distinct return values: one to indicate that the function succeeded and to give the computed result, and another to propagate the error information back to the caller. If there are, say, 5 ways the function could fail, you could need as many as 6 different return values: the “successful computation” return value, and a possibly different package of bits for each of the 5 error cases. Let’s simplify it down to two cases: • “I succeeded and the result is xxx.” • “I failed and the error information is yyy.” Let’s work a simple example: we would like to create a Number class that supports the four arithmetic operations: add, subtract, multiply and divide.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.