c#异常处理示例分享
更新时间:2014年04月02日 10:03:19 作者:
这篇文章主要介绍了c#异常处理示例,需要的朋友可以参考下
复制代码 代码如下:
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#条件拼接Expression<Func<T, bool>>的使用
本文主要介绍了C#条件拼接Expression<Func<T, bool>>的使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2022-02-02
Winform项目中使用FastReport.Net报表控件
这篇文章介绍了Winform项目中使用FastReport.Net报表控件的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-06-06


最新评论