手机
当前位置:查字典教程网 >编程开发 >C#教程 >c#异常处理示例分享
c#异常处理示例分享
摘要:复制代码代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;...

复制代码 代码如下:

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");

}

}

}

}

【c#异常处理示例分享】相关文章:

c# 应用事务的简单实例

C#异常处理详解

C# 批处理调用方法

c# 匿名方法的小例子

c# 文件(夹)创建与删除

c# 值类型实例构造器

c#各种Timer类的区别与用法介绍

C#异步调用的好处和方法分享

c#典型工厂化实现实例

C#常用正则大全分享

精品推荐
分类导航