简体   繁体   English

为什么 C# 中存在未处理的异常错误?

[英]Why ther is an Unhandled Exception Error in C#?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace HelloWorld
{
public class Program
{
public static void Main(string[] args)
{
  int a, b, c;
 Console.WriteLine("give a:");
 a=int.Parse(Console.ReadLine());
 Console.WriteLine("give b:");
 b=int.Parse(Console.ReadLine());
  
  c=a+b;
  Console.WriteLine("c={0}",c);
} 
}
}

The error is this :错误是这样的:

Unhandled Exception:
System.ArgumentNullException: Value cannot be null.
Parameter name: s
  at System.Int32.Parse (System.String s) [0x00003] in <9f0df102fe6e4cfea29d2e46f585d8a5>:0 
  at HelloWorld.Program.Main (System.String[] args) [0x00011] in <47cc63d67af642a680e3be47ee0a2658>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentNullException: Value cannot be null.
Parameter name: s
  at System.Int32.Parse (System.String s) [0x00003] in <9f0df102fe6e4cfea29d2e46f585d8a5>:0 
  at HelloWorld.Program.Main (System.String[] args) [0x00011] in <47cc63d67af642a680e3be47ee0a2658>:0

Why does this error appear?为什么会出现这个错误?

I'm using a mobile compiler.我正在使用移动编译器。

Is it because there are 2 inputs and the mobile compiler can't handle more than one input?是因为有 2 个输入而移动编译器不能处理多个输入吗?

Or is it an error in the code?还是代码中的错误?

It's certainly a conundrum.这当然是一个难题。 Online compilers can be a bit iffy.在线编译器可能有点不确定。 I've just tried the following code on TutorialsPoint.com我刚刚在TutorialsPoint.com上尝试了以下代码

/* Online C# Compiler and Editor */
using System.IO;
using System;

namespace HelloWorld
{
    public class Program
    {
        public static void Main(string[] args)
        {
            int a, b, c;
            Console.WriteLine("give a:");
            a=int.Parse(Console.ReadLine());
            Console.WriteLine("give b:");
            b=int.Parse(Console.ReadLine());
  
            c=a+b;
            Console.WriteLine("c={0}",c);
        } 
    }
}

And it works:它有效:

Output:输出:

give a:
1
give b:
2
c=3

edit编辑

Inclusion of my remark and thinking behind it to the original question:将我的评论和背后的想法包含在原始问题中:

Your original error message:您的原始错误消息:

Parameter name: s at System.Int32.Parse (System.String s) [0x00003] in <9f0df102fe6e4cfea29d2e46f585d8a5>:0 at HelloWorld.Program.Main (System.String[] args) [0x00011] in <47cc63d67af642a680e3be47ee0a2658>:0参数名称:s at System.Int32.Parse (System.String s) [0x00003] in <9f0df102fe6e4cfea29d2e46f585d8a5>:0 at HelloWorld.Program.Main (System.String[] args) [0x00011] in <47cc63d67af642a680e3be47ee0a2658>:0

indicates that there is a parameter called s .表示有一个名为s的参数。 Perhaps the online compiler that you used recognises arg but not args and so sees the s as a parameter of an unknown type.也许您使用的在线编译器识别arg但不识别args ,因此将s视为未知类型的参数。 Try arg instead of args尝试arg而不是args

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM