---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------
try cath异常处理Exception的使用‘
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 异常
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入你的年龄");
int age1 = int.Parse(Console.ReadLine());
try //这个是异常处理 检查我们输入的age 是否正常
{
string desc = GetAgeDesc(age1);
Console.WriteLine(GetAgeDesc(age1));
}
catch(Exception ex)// 这里输出我们异常的age数据得到返回值
{
Console.WriteLine("数据错误+"+ex.Message);
}
Console.ReadKey();
}
static string GetAgeDesc(int age)//这里定义一个方法 来判断我们输入的age 然后给我们输出一个返回值
{
string str;
if (age >= 0 && age <= 10)
{
str = "你的年龄是" + age + "你是婴幼儿";
return str;
}
else if (age > 10 && age < 18)
{
str = "你的年龄是" + age + "你是青少年";
return str;
}
else if (age >= 18 && age < 150)
{
str = "你的年龄是" + age + "你是成年人";
return str;
}
else if (age < 0)
{
throw new Exception("您来自反物质世界吧");
}
else
{
throw new Exception("您见过老佛爷吧");
}
}
}
}