c#異常處理示例分享
更新時(shí)間:2014年04月02日 10:03:19 作者:
這篇文章主要介紹了c#異常處理示例,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
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("輸入錯(cuò)誤:"+ex.Message+"異常堆棧:"+ex.StackTrace);
}
try
{
Console.WriteLine("請(qǐng)輸入你的年齡:");
int s = Convert.ToInt32(Console.ReadLine());
string desc = GetAgeDesc(s);
Console.WriteLine(desc);
}
catch (Exception ex)
{
Console.WriteLine("數(shù)據(jù)錯(cuò)誤,"+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#根據(jù)文件類型獲取相關(guān)類型圖標(biāo)的方法代碼
c#根據(jù)文件類型獲取相關(guān)類型圖標(biāo)的方法代碼,需要的朋友可以參考一下2013-05-05C#對(duì)list列表進(jìn)行隨機(jī)排序的方法
這篇文章主要介紹了C#對(duì)list列表進(jìn)行隨機(jī)排序的方法,涉及C#操作list列表的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04詳解C#設(shè)置Excel數(shù)據(jù)自適應(yīng)行高、列寬的2種情況
這篇文章主要介紹了C#設(shè)置Excel數(shù)據(jù)自適應(yīng)行高、列寬的2種情況,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04C#中的ICustomFormatter及IFormatProvider接口用法揭秘
這篇文章主要介紹了C#中的ICustomFormatter及IFormatProvider接口用法揭秘,本文能過(guò)分析一段代碼得出一些研究結(jié)果,需要的朋友可以參考下2015-06-06淺析C# 使用Process調(diào)用外部程序中所遇到的參數(shù)問(wèn)題
這篇文章主要介紹了C# 使用Process調(diào)用外部程序中所遇到的參數(shù)問(wèn)題,需要的朋友可以參考下2017-03-03C# Winform中如何繪制動(dòng)畫(huà)示例詳解
這篇文章主要給大家介紹了關(guān)于C# Winform中如何繪制動(dòng)畫(huà)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C# Winform具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05