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("數(shù)據(jù)錯誤,"+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("自己創(chuàng)建的ex.Message");
}
}
}
}
相關(guān)文章
C#條件拼接Expression<Func<T, bool>>的使用
本文主要介紹了C#條件拼接Expression<Func<T, bool>>的使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
Winform項目中使用FastReport.Net報表控件
這篇文章介紹了Winform項目中使用FastReport.Net報表控件的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
C#分析URL參數(shù)并獲取參數(shù)和值對應列表的方法
這篇文章主要介紹了C#分析URL參數(shù)獲取參數(shù)和值對應列表的方法,涉及C#進行URL分析及正則表達式的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03

