【问题标题】:How to throw FormatException when the input is int instead of double?当输入是int而不是double时如何抛出FormatException?
【发布时间】:2017-10-04 11:12:59
【问题描述】:

如果 GPA 输入为 int 而不是 double,我必须抛出 FormatException,但我做不到。但是,当在 studentID 字段中输入十进制值时,我能够抛出 FormatException。我所知道的是默认情况下,双数据类型接受 int 值,这就是它不抛出异常的原因,但我需要确保作为 GPA 输入的值是双精度值。

using System;
using static System.Console;
// Declare a Student
// ID must be an integer and gpa must be a double to continue
namespace Debug4_4
{
    class Debug4_4
    {
        static void Main()
        {
            Student stu = new Student();
            bool areNumbersGood = false;
            while (!areNumbersGood)
            {
                try
                {
                    stu.setID();
                    stu.setGPA();
                    areNumbersGood = true;
                }
                catch (FormatException e)
                {
                    WriteLine(e.Message);
                    WriteLine("(Either the student ID or the GPA");
                    WriteLine(" was not in the correct format.)");
                    WriteLine("You will have to re-enter the student data.");
                }
            }
            WriteLine("Valid student");
        }
    }
    public class Student
    {
        private int stuId;
        private double stuGpa;
        public void setID()
        {
            string stuNumber;
            try
            {
                Write("Enter student ID ");
                stuNumber = ReadLine();
                stuId = Convert.ToInt32(stuNumber);
            }
            catch (FormatException fe)
            {
                throw (fe);
            }
        }

        //throw (fe);
        //}
        public void setGPA()
        {
            string stuGPAString;
            //string stuGPAString;
            try
            {
                Write("Enter student GPA ");
                stuGPAString = ReadLine();
                stuGpa = Convert.ToDouble(stuGPAString);

            }
            catch (FormatException fe)
            {
                throw (fe);
            }
        }
    }
}

【问题讨论】:

  • 学生不能有 4 GPA 或 5 GPA 吗?如果用户输入的 int 值可以转换为 double 为什么要进行这种验证?
  • stuGpa IS 是双重的。究竟是什么问题?您已经声明您认识到int 可以转换为double,并且您将输入存储在double 字段中。所以你已经确保输入是double
  • 闻起来像家庭作业。与其捕获异常来检测错误,不如尝试 Integer.TryParse 并在失败时抛出您自己的异常。
  • 另外,如果你的catch 的唯一目的是throw 确切的例外,那么你真的根本不需要try/catch 块。他们什么也没做。

标签: c# exception-handling formatexception


【解决方案1】:

我假设当您说输入必须是双倍时,您期望输入具有像 4.0 而不是 4 这样的小数。这是一个奇怪的要求,但如果这是您正在寻找的,那么您可以通过做这样的事情来实现它。可能有更好的选择,但这也可以

if (!(Double.TryParse(stuGPAString, out stuGpa) && stuGPAString.LastIndexOf(".") < (stuGPAString.Length - 1)))
{
    throw new FormatException("Invalid gpa");
};

但如果你只想验证 gpa 是否为 Double,如果不是则抛出 FormatException,然后使用这个

if (!Double.TryParse(stuGPAString, out stuGpa))
{
   throw new FormatException("Invalid gpa");
};

所以你的 setGPA 方法看起来像

    public void setGPA()
    {
        string stuGPAString;

        if (!Double.TryParse(stuGPAString, out stuGpa))
        {
            throw new FormatException("Invalid student gpa");
        };
    }

【讨论】:

    【解决方案2】:

    我认为你可以使用 GetType() 方法。

    var x=10;
    x.GetType(); => int
    var x=10.0;
    x.GetType(); => double
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-03
      • 1970-01-01
      相关资源
      最近更新 更多