C + + std::invalid_argument application _c language

Source: Internet
Author: User
First, the invalid_argument is a class (class invalid_argument;), and its inheritance relationship is as follows

Exception-------->logic_error--------->invalid_argument

Invalid_argument prototype is
Copy Code code as follows:

Class Invalid_argument:public Logic_error {
Public
Explicit invalid_argument (const string& WHAT_ARG);
};

It is in the stdexcept header file, within the Std namespace. Here's an example to use it
Copy Code code as follows:

#include <iostream>
#include <stdexcept>

int main (int argc,char * * argv)
{
Try
{
BOOL Errorargument;
Errorargument=true;
if (errorargument)
{
Throw Std::invalid_argument ("occur error!");
}
}
catch (Std::invalid_argument &ia)
{
What () a function that inherits the exception class for invalid_argument
std::cerr<< "Invalid_argument" << ia.what () <<std::endl;
}

return 0;
}

The results of the operation are:

Invalid_argument occur error! So the above example is one of the simplest applications. Invalid_argument, as the name implies, refers to invalid parameters, this should be applied to check whether the parameters are invalid, the general check parameters for specific functions and classes, then it should be to the class member variable assignment or function parameter assignment, check the value assigned to them is valid, For example, there is a class (people, with three member variables name,age,height) Then we know the age of people between 0~150岁 (PS: If the programmer can be directly defined as 0~75). Height words 0~300cm, the length of the name will not exceed 20. If all these ranges are exceeded, it can be assumed that the data is invalid. Then this class can be defined as follows:
Copy Code code as follows:

#include <stdexcept>
#include <iostream>
#include <string>

class people
{
Public
People (const std::string& n,const int& a,const int& h)
: Name (n), age (a), height (h)
{}

inline void Set (const std::string& n,const int& a,const int& h)
{
if (!valid (n,a,h))
{
Throw std::invalid_argument ("People ' s argument is error");
}
name = N;
age = A;
Height = h;
}

inline bool Valid (const std::string& N, const int& A, const int& h)
{
Return (n.length () = = 0 | | N.length () >) && a >= 0 && a< && h > 0 && H < 300;
}
Private
std::string name;
int age;
int height;

};

int main (int argc, char** argv)
{
People P ("Li San", 20, 170);
Try
{
P.set ("Li San", 20, 1700);
}
catch (Std::invalid_argument & IA)
{
Std::cerr << "Error:" << ia.what () << Std::endl;
}
return 0;
}

The results of the operation are:

Error:people ' s argument is error the above program will output errors as long as the invalid data is entered. But this is not enough, we can not locate the invalid parameter in which file with which row or in which function, if the print error when the output of the same information to believe that the positioning problem is much more convenient. Then we have a lot more to add to this information when we're reporting the wrong message.
Copy Code code as follows:

#include <stdexcept>
#include <iostream>
#include <string>
#define TOSTRING (x) #x

Class ErrorInfo
//{
Public
ErrorInfo (const std::string& f,const std::string& l,const std::string& Fun)
: File (f), line (L), func (fun)
// {}
//
Inline const std::string GetFile () const
// {
Return this->file;
// }
//
Inline const std::string getline () const
// {
Return this->line;
// }
//
Inline const std::string GETFUNC () const
// {
Return this->func;
// }
//
Private
const std::string file;
const std::string Line;
Const std::string func;
//};

Class ErrorInfo
{
Public
ErrorInfo (const char * f, const char * l, const char * fun)
: File (f), line (L), func (fun)
{}

Inline std::string getFile () const
{
Return this->file;
}

Inline std::string getline () const
{
Return this->line;
}

Inline std::string getfunc () const
{
Return this->func;
}
Private
const char* file;
Const char* Line;
Const char* func;
};

std::string operator + (const std::string & str, const errorinfo& EI)
{
Std::string strtemp (Ei.getfile () + ":" + ei.getline () + ":" + Ei.getfunc ());
strtemp +=str;
return strtemp;
Return str::string (Ei.getfile () + ":" + ei.getline () + ":" + ei.getfunc () + + str);
}

Class Invalidpeople:public Std::invalid_argument
{
Public
Invalidpeople (errorinfo & ei)
: Std::invalid_argument ("Invalid people" + ei)
{}
~invalidpeople () throw ()
{}
};

class people
{
Public
People (const std::string& n,const int& a,const int& h)
: Name (n), age (a), height (h)
{}

inline void Set (const std::string& n,const int& a,const int& h)
{
if (!valid (n,a,h))
{
ErrorInfo ei (__file__,tostring (__line__), __pretty_function__);
ErrorInfo ei (__file__, #__LINE__, __pretty_function__);
Throw invalidpeople (EI);
Throw Invalidpeople (ErrorInfo (__file__,tostring (__line__), __pretty_function__));
}
name = N;
age = A;
Height = h;
}

inline bool Valid (const std::string& N, const int& A, const int& h)
{
Return (n.length () = = 0 | | N.length () >) && a >= 0 && a< && h > 0 && H < 300;
}
Private
std::string name;
int age;
int height;

};

int main (int argc, char** argv)
{
People P ("Li San", 20, 170);
Try
{
P.set ("Li San", 20, 1700);
}
catch (Std::invalid_argument & IA)
{
Std::cerr << "Error:" << ia.what () << Std::endl;
}
return 0;
}

The results of the operation are:

Testerror:invalid_a.cpp:__line__:void people::set (const std::string&, const int&, const int&) invalid People Note:
(1) The above #define TOSTRING (x) #x就可以将int类型转换为const char * type.
(2) The __file__ is a const char* type, and the type that is converted by #define TOSTRING (x) is a const char* type, and the compiler is gun so that the function name is the __PRETTY_FUNCTION__ is also a const char* type.
You can see that __line__ does not show the travel number, and here's why the compiler directly converts __line__ to the string "__line__." So how does this work out? The author has tried many methods, finally found out, we can in #define once, can be normal reality. The following code
Copy Code code as follows:

#include <stdexcept>
#include <iostream>
#include <string>
#define TTOSTRING (x) #x
#define TOSTRING (x) ttostring (x)
...
...
The following code is the same as above

The results of the operation are:

Testerror:invalid_a.cpp:91:void people::set (const std::string&, const int&, const int&) Invalid people
As for the inside principle, why can two __line__ represented by the number of such as (71) into a const char* "71", instead of converting to "__line__" Const char* string, hope that clear friends, give the answer, thank you!
Related Article

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.