using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class MyException:ApplicationException
{
public MyException(string msg) : base(msg) { }
}
class Program
{
static void Main(string[] args)
{
try{
string t = Console.ReadLine();
char[] m = t.ToCharArray();
foreach (char item in m)
{
if (item == '.')
throw new MyException("输入的数据应为整数。");
}
int sum = 1, n = int.Parse(t);
do
{
sum *= n;
}
while (--n != 0);
Console.WriteLine("阶乘为:{0}", sum);
Console.ReadKey();
}
catch (MyException e)
{
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("按任意键退出 …");
Console.ReadKey();
}
}
}
}
C#自定义异常类
最新推荐文章于 2024-03-19 17:50:22 发布