C + + Exception return error code

Source: Internet
Author: User

A more flexible approach than terminating an exception is to use the return value of the function to indicate the problem. For example, the Get (void) member ASCII code of the Ostream class, but when the end of the file is reached, the special value EOF is returned. For Hmean (), this method does not work. Any resin is a valid return value, so there is no special value that can be used to indicate the problem. In this case, you can use pointer parameters or reference parameters to return values to the calling program and use the return value of the function to indicate success or failure. IStream-family Overloads >> operators use variants of this technique. By telling the caller whether the program succeeded or failed, the program can take other measures other than the abnormal termination procedure. The following program is an example that uses this method to redefine the return value of Hmean () to bool, to indicate whether the return value indicates success or failure, and to add a third parameter to the function to provide an answer.
Error2.cpp

//Error2.cpp--Returning an error code#include <iostream>#include<cfloat>//(or float.h) for Dbl_maxBOOLHmean (DoubleADoubleBDouble*ans);intMain () {Doublex, y, Z; Std::cout<<"Enter The numbers:";  while(Std::cin >> x >>y) {if(Hmean (x, Y, &z)) std::cout<<"Harmonic mean of"<< x <<" and"<<y<<" is"<< Z <<Std::endl; ElseStd::cout<<"One value should not being the negative"<<"Of the other-try again.\n"; Std::cout<<"Enter Next set of numbers <q to Quit>:"; } std::cout<<"bye!\n"; return 0;}BOOLHmean (DoubleADoubleBDouble*ans) {    if(A = =-b) {*ans =Dbl_max; return false; }    Else    {        *ans =2.0* A * b/(A +b); return true; }}

Effect:

Enter numbers:3 6Harmonic mean of 3 and 6 is 4Enter next set of numbers <q to Quit>: 10-10one value should not Be the negative of the other-try again. Enter next set of numbers <q to Quit>: 1 19Harmonic mean of 1 and are 1.9Enter next set of numbers <q to QUIT&G t;: qbye!

Another way to store return conditions somewhere is to use a global variable. A function that may be problematic can set the global variable to a specific value when a problem occurs, and the calling program can examine the variable. The traditional C language Math library uses this method, which uses the global variable named errno. Of course, you must ensure that other functions do not use the global variable for other purposes.

C + + Exception return error code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.