【问题标题】:System.FormatException in WriteLineWriteLine 中的 System.FormatException
【发布时间】:2015-10-16 03:47:08
【问题描述】:

我的代码遇到了这个异常:

System.FormatException

附加信息:输入字符串的格式不正确。

我的 Visual Studio C# 解决方案中有两个文件:

  1. 程序.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EventPubSub
    {
        class Program
        {
            static void Main(string[] args)
            {
                Rectangle rect = new Rectangle();
                // Subscribe to the Changed event
                rect.Changed += new EventHandler(Rectangle_Changed);
                rect.Length = 10;
            }
            static void Rectangle_Changed(object sender, EventArgs e)
            {
                Rectangle rect = (Rectangle)sender;
                Console.WriteLine("Value Changed: Length = { 0}", rect.Length);
            }
        }
    }
    
  2. 文件Rectangle.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EventPubSub
    {
        class Rectangle
        {
            //Declare an event named Changed of
            //delegate type EventHandler
    
            public event EventHandler Changed;
    
            private double length = 5;
    
            public double Length
            {
                get
                {
                    return length;
                }
                set
                {
                    length = value;
                    //Publish the Changed event
                    Changed(this, EventArgs.Empty);
                }
            }
        }
    }
    

执行该行时出现异常:rect.Length = 10;

【问题讨论】:

  • FormatException 不是由于int.Parse("bob") 引起的罕见情况...

标签: c# console.writeline formatexception


【解决方案1】:

处理程序中{0 之间有一个空格导致异常

【讨论】:

    【解决方案2】:

    请像这样更改事件处理程序,一切都会好起来的

    static void Rectangle_Changed(object sender, EventArgs e)
    {
        Rectangle rect = (Rectangle)sender;
        Console.WriteLine(string.Format("Value Changed: Length = {0}", rect.Length));
    }
    

    我在这里做了 2 处更改 -

    1. 添加了一个string.Format(这不是问题)

    2. 删除了{0 之间的空格。它是{ 0} 现在我做到了{0}(这是实际问题)

    【讨论】:

    • Console.WriteLine(String.Format(...)) 是毫无意义的重复,但否则很好的答案。
    • @AlexeiLevenkov 是对的。有无字符串格式都是一样的。添加它毫无意义,因为它们是等效的。这里的错误只是 { 和 0 之间的空格。
    • @AlexeiLevenkov,哦,是的!当然......我会编辑我的答案以突出这一点
    【解决方案3】:

    尝试首先添加try catch 以捕获它抛出的错误。所以你可以识别并修复它。这只是为了帮助您下次解决自己的问题。 :)

    static void Main(string[] args)
      {
         Rectangle rect = new Rectangle();
         string errorMessage = String.empty;
         try
         {
              // Subscribe to the Changed event
              rect.Changed += new EventHandler(Rectangle_Changed);
              rect.Length = 10;
          }
          catch(Exception ex)
          {
               errorMessage = ex.Message;
          }
      }
    

    【讨论】:

    • 这样做后我收到此消息:输入字符串格式不正确。
    • 那么这意味着您的问题出在消息部分。我只是在这里教你下次如何处理这种错误。 “授人以鱼不如授人以渔”。 ;)
    猜你喜欢
    • 1970-01-01
    • 2013-02-09
    • 2015-02-21
    • 2011-07-17
    • 2018-11-09
    • 2013-05-09
    • 2016-01-11
    • 2021-06-18
    • 1970-01-01
    相关资源
    最近更新 更多