• try-catch的使用(简单用法 )


    c#中异常捕获

    语法:

    try

    {

    有可能出现错误的代码写在这里

    }

    catch

    {

    出错后的处理

    }

    如果try中的代码没有出错,则程序正常运行try中的内容后,不会执行catch中的内容,

    如果try中的代码一但出错,程序立即跳入catch中去执行代码,那么try中出错代码后的所有代码就不再执行了.


    例1:

    try

    {

    Console.WriteLine("请输入你的语文成绩");

    int chineseScore=Convert.ToInt32(Console.ReadLine());

    Console.WriteLine("请输入你的数学成绩");

    int mathScore=Convert.ToInt32(Console.ReadLine());

    Console.WriteLine("你的总成绩是:{0} 你的平均成绩是:{1}",chineseScore+mathScore,(chineseScore+mathScore)/2);

    }

    catch

    {

    Console.WriteLine("你输入的有误,请再次运行后输入");

    }

    Console.ReadKey();


    例2:

    try

    {

    Console.WriteLine("请输入你的姓名");

    string name=Console.ReadLine();

    Console.WriteLine("请输入语文成绩");

    int chineseScore=Convert.ToInt32(Console.ReadLine());

    Console.WriteLine("请输入数学成绩");

    int mathScore=Convert.ToInt32(Console.ReadLine());

    Console.WriteLine("请输入英语成绩");

    int englishScore=Convert.ToInt32(Console.ReadLine());

    Console.WriteLine("{0}你的总分为{1}分,平均为{2}分",name,chineseScore+mathScore+englishScore,(chineseScore+mathScore+englishScore)/3);

    }

    catch

    {

    Console.WriteLine("你输入的有误,请重新运行本程序");

    }

    Console.ReadKey();


    例3.1,

    try

    {

    Console.WriteLine("请输入你要计算的天数");

    int days=Convert.ToInt32(Console.ReadLine()); 

    int week=days/7;

    int number=days%7; 

    Console.WriteLine("{0}是{1},零{2}",days,week,number);

    }

    catch

    {

    Console.WriteLine("你输入有误,请重新启动程序再输一次");

    }

    Console.ReadKey();

    例3.2:

    try

    {

    Console.WriteLine("请输入你要计算的天数");

    int days=Convert.ToInt32(Console.ReadLine());

    int month=days/30;

    int week=(days-month*30)/7;

    int dayNumber=days-month*30-week*7;

    Console.WriteLine("{0}天中有{1}月,{2}周,{3}天.",days,month,week,dayNumber);

    }

    catch

    {

    Console.WriteLine("你输入的有误,请重新输入一次");

    }

    Console.ReadKey();

    上题也可以这样书写代码:

    try

    {

    Console.WriteLine("请输入你要计算的天数");

    int days=Convert.ToInt32(Console.ReadLine());

    int month=days/30;

    int mot=days%30;//除去上面一个月30天的天数后还剩多少天.

    int week=mot/7;//看看剩下的天数中有多少个周。

    int dayNumber=mot%7;//除去上面一个周7天的天数后还剩多少天.

    Console.WriteLine("{0}天中有{1}月,{2}周,{3}天",days,month,week,dayNumber);

    }

    catch

    {

    Console.WriteLine("你输入的有误,请重新输入");

    }

    Console.ReadKey();

    例3.3

    try

    {

    Console.WriteLine("请输入你要转换的秒数");

    int seconds=Convert.ToInt32(Console.ReadLine());

    int days=seconds/(3600*24);//总秒数除以1天的秒数就得出有多少天数了

    int mod=seconds%(3600*24);//看看除去上面秒数还剩下多少秒

    int hours=mod/3600;//用上面一步剩下的秒除以1小时的秒数就得出有多少小时了

    mod=mod%3600;//看看除去上面一步秒数还剩下多少秒

    int min=mod/60;//用上面一步剩下的秒数除以1分钟的秒数就得出有多少分钟了

    int second=mod%60;看看除去上面一步秒数还剩下多少秒

    Console.WriteLine("{0}中共有{1}天,{2}小时,{3}分,{4}秒",seconds,days,hour,min,second);

    }

    catch

    {

    Console.WriteLine("你输入有误,请重新输入");

    }

    Console.ReadKey();

  • 相关阅读:
    全网最深分析SpringBoot MVC自动配置失效的原因
    这一次搞懂Spring代理创建及AOP链式调用过程
    这一次搞懂SpringBoot核心原理(自动配置、事件驱动、Condition)
    这一次搞懂Spring Web零xml配置原理以及父子容器关系
    这一次搞懂SpringMVC原理
    这一次搞懂Spring事务是如何传播的
    Redis系列(八):发布与订阅
    【深度思考】JDK8中日期类型该如何使用?
    【Redis面试题】如何使用Redis实现微信步数排行榜?
    Nacos系列(一):Nacos环境安装及Hello World示例
  • 原文地址:https://www.cnblogs.com/swlq/p/5355341.html
Copyright © 2020-2023  润新知