栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++实用异常

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++实用异常

关于C++的实用异常研究

目录
    • 关于C++的实用异常研究
        • 1. 简单输出自己设定的提示信息

1. 简单输出自己设定的提示信息

(1)问题描述
在异常抛出领域,刚开始很难记得清楚都要抛出什么错误,不如就简单的抛出一下自己设定的提示语句,也就是说执行到某一句如果出错,简单抛出提示语句,继续执行后边的内容。
(2)代码部分
这里简单用了几个文件分开写了一下。

circle.h

#ifndef CALCULATEFUN_TIME_CIRCLE_H
#define CALCULATEFUN_TIME_CIRCLE_H
# include 

void sayHello(int num);

#endif //CALCULATEFUN_TIME_CIRCLE_H

circle.cpp

# include "circle.h"
void sayHello(int num){
    for (int i = 0; i < num; ++i) {
        std::cout << "hello world" << std::endl;
    }
    throw "hello, here is the try-catch strure";
}

main.cpp

#include 
#include 
#include "circle.h"
int main() {
    unsigned long time_1 = 0, time_2 = 0;

    time_1 = GetTickCount();    // calculate the start time
    try{
        sayHello(2);
    }catch (const char * str){
        std::cout<< str << std::endl;   // try-catch test
    }
    time_2 = GetTickCount();    // calculate the end time

    std::cout<< (time_2 - time_1) << std::endl;
    return 0;
}

(3)运行结果

(4)代码分析
注意,这里我加了一个计算函数运行时间的函数 GetTickCount(),单位以ms计算。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1010092.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号