• C#学习笔记六异常


    异常

     

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

     

    namespace 异常

    {

        class Program

        {

            static void Main(string[] args)

            {

                try

                {

                    int i = Convert.ToInt32("abc");

                }

                catch (Exception e)

                {

                    Console.WriteLine("发生错误:" + e.Message + "\n异常堆栈:" + e.StackTrace);//这样就可以直接打印出发生异常的原因和位置

                }

                Console.ReadKey();

            }

        }

    }

     

     

     

    namespace 抛出自己的异常

    {

        class Program

        {

            static void Main(string[] args)

            {

                try

                {

                    string desc = AgeDescribe(300);

                }

                catch(Exception e)

                {

                    Console.WriteLine(e.Message);

                }

            }

            static string AgeDescribe(int age)

            {

                if (age >= 0 && age < 3)

                {

                    return "婴儿";

                }

                else if (age >= 3 && age < 18)

                {

                    return "青少年";

                }

                else if (age >= 18 && age < 150)

                {

                    return "成年人";

                }

                else if (age >= 150)

                {

                    throw new Exception("你是神仙吧!");

                }

                else if (age < 0)

                {

                    throw new Exception("你来自反物质吧!");

                }

                return "0";

            }

           

        }

    }

  • 相关阅读:
    两万字!多线程50问!
    【微服务】:何为微服务、网关、服务发现/注册?
    一万了解 Gateway 知识点
    推荐收藏系列:一文理解JVM虚拟机(内存、垃圾回收、性能优化)解决面试中遇到问题
    1.3w字,一文详解死锁!
    7.3万字肝爆Java8新特性,我不信你能看完!(建议收藏)
    三万字+八十图,详解Redis五十二问!太全面了
    7_布局管理器.md
    5_资源文件.md
    1_Qt简介.md
  • 原文地址:https://www.cnblogs.com/tangzhengyue/p/2152396.html
Copyright © 2020-2023  润新知