自定义异常类

来源:互联网 发布:电子商务中的网络指 编辑:程序博客网 时间:2024/06/05 10:37
/*将百分制转换为五分制,如果输入的百分制成绩超出0~100时,程序抛出异常。*/using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.Linq;using System.Text;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            try            {                Console.WriteLine("请输入成绩:");                double score = double.Parse(Console.ReadLine()); ;                if (score < 0 || score > 100)                    throw new outofBoundException("成绩输入有误!");                Console.WriteLine(score * 5 / 100);            }            catch (FormatException)            {                Console.WriteLine("必须为数字!");            }            catch (outofBoundException e)            {                Console.WriteLine(e.Message);            }            catch (Exception e)            {                Console.WriteLine(e.Message);            }            finally            {                Console.ReadKey();            }        }    }}class outofBoundException : ApplicationException{    public outofBoundException(string message)        : base(message)    { }}


 

0 0
原创粉丝点击