欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#异常处理示例分享

程序员文章站 2024-02-23 12:42:40
复制代码 代码如下:using system;using system.collections.generic;using system.linq; using syste...
复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq; using system.text;
//2014.3.14
namespace _6.异常
{
    class program
    {
        static void main(string[] args)
        {
            try
            {
                console.writeline("convert之前");
                int a = convert.toint32(console.readline());
                console.writeline("convert之后");
            }
            catch (exception ex)
            {
                console.writeline("输入错误:"+ex.message+"异常堆栈:"+ex.stacktrace);
            }

            try
            {
                console.writeline("请输入你的年龄:");
                int s = convert.toint32(console.readline());
                string desc = getagedesc(s);
                console.writeline(desc);
            }
            catch (exception ex)
            {
                console.writeline("数据错误,"+ex.message);
            }
             console.readkey();
        }

        static string getagedesc(int age)
        {
            if (age >= 0 && age <= 3)
            {
                return "婴幼儿";
            }
            else if (age > 3 && age < 18)
            {
                return "青少年";
            }
            else if (age >=18 && age < 60)
            {
                return "成年人";
            }
            else if (age >= 60 && age < 100)
            {
                return "老年人";
            }
            else
            {
                throw new exception("自己创建的ex.message");
            }
        }
    }
}