C ++ std: invalid_argument Application

Source: Internet
Author: User

First, it indicates that invalid_argument is a class (class invalid_argument;), and its inheritance relationship is as follows:

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

The invalid_argument prototype isCopy codeThe Code is as follows: class invalid_argument: public logic_error {
Public:
Explicit invalid_argument (const string & what_arg );
};

It is in the stdexcept header file and in the std namespace. The following is an example to use it.Copy codeThe Code is 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 () is an invalid_argument that inherits the functions of the exception class
Std: cerr <"Invalid_argument" <ia. what () <std: endl;
}

Return 0;
}

The running result is:

Invalid_argument occur error! The above example is the simplest application. Invalid_argument, as its name implies, indicates an invalid parameter. This parameter should be used to check whether the parameter is invalid. Generally, it is used for specific functions and classes, when assigning values to member variables or function parameters of a class, check whether the values assigned to them are valid. For example, there is a class (people, there are three member variables name, age, height) so we know that the person's age is 0 ~ Between 150 years old (ps: can be directly defined as 0 ~ for programmers ~ 75 ). Height 0 ~ 300, the name length cannot exceed 20. If the data exceeds these limits, it can be regarded as invalid data. This class can be defined as follows:Copy codeThe Code is 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 =;
Height = h;
}

Inline bool valid (const std: string & n, const int & a, const int & h)
{
Return (n. length () = 0 | n. length ()> 20) & a> = 0 & a <150 & 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,170 0 );
}
Catch (std: invalid_argument & ia)
{
Std: cerr <"Error:" <ia. what () <std: endl;
}
Return 0;
}

The running result is:

Error: People's argument is error the above program will output an Error as long as the input of invalid data. However, this is not enough. We still cannot locate the file with which the invalid parameter is located or in which function, if this information is output together when an error is printed, it is much easier to locate the problem. Therefore, when an error message is reported, it is much clearer to add the information.Copy codeThe Code is 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 =;
Height = h;
}

Inline bool valid (const std: string & n, const int & a, const int & h)
{
Return (n. length () = 0 | n. length ()> 20) & a> = 0 & a <150 & 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,170 0 );
}
Catch (std: invalid_argument & ia)
{
Std: cerr <"Error:" <ia. what () <std: endl;
}
Return 0;
}

The running result is:

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 can convert the int type to the const char * type.
(2) _ FILE _ is of the const char * type, and the type converted by # define TOSTRING (x) is of the const char * type, if the compiler is gun, the name of the function to be retrieved is _ PRETTY_FUNCTION _, which is also of the const char * type.
We can see that _ LINE _ does not display the travel number, here, the compiler directly converts _ LINE _ to the string "_ LINE _". How can this problem be solved? I tried a lot of methods and finally found it out. We can find it at # define once, so that we can be normal. The following codeCopy codeThe Code is 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 running result is:

TestError: invalid_a.cpp: 91: void People: set (const std: string &, const int &, const int &) Invalid People
Why can we convert the number represented by _ LINE _ (for example, (71) to const char * "71" twice ", instead of converting it to the "_ LINE _" const char * string, hope you can give the answer clearly. Thank you!

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.