zoukankan      html  css  js  c++  java
  • 使用系统提供的标准异常

    标准异常库

    • #incldue <stdexcept>
    • throw out_of_range(”aaa”) 。。。
    • catch(out_of_range & e)  cout  <<  e.what();
    #define _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    #include <string>
    #include <stdexcept>    //标准异常库
    using namespace std;
    //exception
    
    class Person
    {
    public:
        Person(string name, int age)
        {
            this->m_Name = name;
            if (age < 0 || age>200)
            {
                throw out_of_range("年龄越界了!");
            }
            else
            {
                this->m_Age = age;
            }
        }
    
        string m_Name;
        int m_Age;
    };
    void test01()
    {
        try
        {
            Person p1("张三", 300);
        }
        catch (exception& e)    //使用系统提供的标准异常 多态
        {
            cout << e.what() << endl;
        }
    }
    
    int main()
    {
        test01();
        system("Pause");
        return 0;
    }

    结果:

  • 相关阅读:
    2.13 day 10
    2.12 day9
    Mongo基础知识
    给mongodb设置密码
    前端框架
    SecureCRT的一些设置
    node 服务器框架
    python 知识博客
    数据库记录
    有用的网站
  • 原文地址:https://www.cnblogs.com/yifengs/p/15184690.html
Copyright © 2011-2022 走看看