c#異常處置示例分享。本站提示廣大學習愛好者:(c#異常處置示例分享)文章只能為提供參考,不一定能成為您想要的結果。以下是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");
}
}
}
}