<div class="blogpost-body" id="cnblogs_post_body">
<p class="p0"><span style="font-family:'微软雅黑';font-size:16px;">首先说明invalid_argument是一个类(class invalid_argument;),它的继承关系如下</span></p>
<p class="p0"><span style="font-family:'微软雅黑';font-size:16px;"><span style="line-height:1.5;">exception-------->logic_error--------->invalid_argument </span></span></p>
<p class="p0"><span style="font-family:'微软雅黑';font-size:16px;"><span style="line-height:1.5;">invalid_argument原型是</span></span></p>
<div class="cnblogs_code">
<pre class="blockcode"><span style="color:#008080;">1</span> <span style="color:#0000ff;">class</span> invalid_argument:<span style="color:#0000ff;">public</span><span style="color:#000000;"> logic_error {
</span><span style="color:#008080;">2</span> <span style="color:#0000ff;">public</span><span style="color:#000000;">:
</span><span style="color:#008080;">3</span> <span style="color:#0000ff;">explicit</span> invalid_argument (<span style="color:#0000ff;">const</span> <span style="color:#0000ff;">string</span>&<span style="color:#000000;"> what_arg);
</span><span style="color:#008080;">4</span> };</pre>
</div>
<p><code><dfn></dfn></code><span style="font-family:'微软雅黑';font-size:16px;line-height:1.5;">它在stdexcept头文件中,在std命名空间内。下面举一个例子来使用它</span></p>
<div class="cnblogs_code">
<pre class="blockcode"><span style="color:#008080;"> 1</span> #include <iostream>
<span style="color:#008080;"> 2</span> #include <stdexcept>
<span style="color:#008080;"> 3</span>
<span style="color:#008080;"> 4</span> <span style="color:#0000ff;">int</span> main(<span style="color:#0000ff;">int</span> argc,<span style="color:#0000ff;">char</span> **<span style="color:#000000;"> argv)
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;">{
</span><span style="color:#008080;"> 6</span> <span style="color:#0000ff;">try</span>
<span style="color:#008080;"> 7</span> <span style="color:#000000;"> {
</span><span style="color:#008080;"> 8</span> <span style="color:#0000ff;">bool</span><span style="color:#000000;"> errorArgument;
</span><span style="color:#008080;"> 9</span> errorArgument=<span style="color:#0000ff;">true</span><span style="color:#000000;">;
</span><span style="color:#008080;">10</span> <span style="color:#0000ff;">if</span><span style="color:#000000;">(errorArgument)
</span><span style="color:#008080;">11</span> <span style="color:#000000;"> {
</span><span style="color:#008080;">12</span> <span style="color:#0000ff;">throw</span> std::invalid_argument(<span style="color:#800000;">"</span><span style="color:#800000;">occur error!</span><span style="color:#800000;">"</span><span style="color:#000000;">);
</span><span style="color:#008080;">13</span> <span style="color:#000000;"> }
</span><span style="color:#008080;">14</span> <span style="color:#000000;"> }
</span><span style="color:#008080;">15</span> <span style="color:#0000ff;">catch</span>(std::invalid_argument &<span style="color:#000000;">ia)
</span><span style="color:#008080;">16</span> <span style="color:#000000;"> {
</span><span style="color:#008080;">17</span> <span style="color:#008000;">//</span><span style="color:#008000;">what()为invalid_argument继承exception类的函数</span>
<span style="color:#008080;">18</span> std::cerr<<<span style="color:#800000;">"</span><span style="color:#800000;"> Invalid_argument </span><span style="color:#800000;">"</span><< ia.what()<<<span style="color:#000000;">std::endl;
</span><span style="color:#008080;">19</span> <span style="color:#000000;"> }
</span><span style="color:#008080;">20</span>
<span style="color:#008080;">21</span> <span style="color:#0000ff;">return</span> <span style="color:#800080;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;">22</span> }</pre>
</div>
<p><span style="font-family:'微软雅黑';font-size:16px;">运行结果为:</span></p>
<div class="cnblogs_code">
<pre class="blockcode">Invalid_argument occur error!</pre>
</div>
<p><span style="font-family:'微软雅黑';font-size:16px;">那么上面的例子是一个最简单的应用了。invalid_argument顾名思义指无效参数,这个应该应用在检查参数是否是无效的,一般检查参数用于特定的函数以及类,那么就应该是给类的成员变量赋值或者函数参数赋值时,检查其赋给它们的值是否有效,例如有一个类(people,有三个成员变量name,age,height)那么我们知道人的年龄在0~150岁之间(ps:如果对于程序员可以直接定义为0~75)。身高的话0~300cm,名字的长度不会超过20。如果都超过这些范围,就可以认定是无效数据。那么这个类可以如下定义:</span></p>
<div class="cnblogs_code">
<pre class="blockcode"><span style="color:#008080;"> 1</span> #include <stdexcept>
<span style="color:#008080;"> 2</span> #include <iostream>
<span style="color:#008080;"> 3</span> #include <<span style="color:#0000ff;">string</span>>
<span style="color:#008080;"> 4</ |
|