您的位置:首页 > 编程语言 > C#

C#入门11.7_本章小结及任务实施

2016-09-05 21:33 253 查看
实例:

让用户输入一个数,然后求该数的倒数

根据用户输入内容的不同产生相应异常提示

1.除零异常

2.溢出异常

3.转换异常

4.其他未知异常

public static void RunSnippet()
{
Console.WriteLine("请输入一个整数:");
try
{
int myint=int.Parse(Console.ReadLine());
double mydouble=1.0/myint;
Console.WriteLine("该数的倒数是:"+mydouble);
}
catch(DivideByZeroException)
{
Console.WriteLine("产生除零异常");
}
catch(OverflowException)
{
Console.WriteLine("溢出异常");
}
catch(FormatException)
{
Console.WriteLine("转换异常");
}
catch(Exception)
{
Console.WriteLine("其他异常");
}
}英雄联盟真好玩
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: