当对象为空时不抛出异常。

huangapple go评论78阅读模式
英文:

Not throwing exception when object is null

问题

public class Stack
{
public readonly Stack foodStack = new Stack();
public void Push(object obj)
{
if (obj == null)
{
throw new InvalidOperationException("您不能将空对象添加到堆栈");
}

    string foodObject = obj.ToString();
    foodStack.Push(foodObject);
}

}

主要方法:

Stack item1 = new Stack();
item1.Push(null);

我尝试过更改异常的类型。

英文:

So I created a basic stack which takes in a string and gets passed in an object. I want to make sure that if the object being passed in is null, it throws an exception. However when I pass in a null object, the exception doesn't get thrown and can't see why.

public class Stack
{
    public readonly Stack<string> foodStack = new Stack<string>();
    public void Push(object obj)
    {
        if (obj == null)
        {
            throw new InvalidOperationException("You cannot add null to the stack");
        }

        string foodObject = obj.ToString();
        foodStack.Push(foodObject);
    }

Main Method:

Stack item1 = new Stack();
item1.Push(null);

I have tried changing the type of exception.

答案1

得分: 1

你的环境/目标是什么?

// .NET FW 4.8, C# 5.0
using System;
using System.Collections.Generic;
namespace NsCustomStack {
    public class Astack {
        public readonly Stack<string> foodStack = new Stack<string>();
        public void Push(object obj) {
            if (obj == null) {
                throw new InvalidOperationException("You cannot add null to the stack");
            }
            string foodObject = obj.ToString();
            foodStack.Push(foodObject);
        }
    }
    class CustomStack {
        static void Main() {
            Astack item1 = new Astack(); item1.Push(null);
        }
    }
}

结果:

未处理的异常: System.InvalidOperationException: 不能向堆栈中添加空值

___ 在 NsCustomStack.Astack.Push(Object obj) 中

___ 在 NsCustomStack.CustomStack.Main() 中

更新 我的猜测是

Stack item1 = new Stack();
item1.Push(null);

使用了 System.Collections.Stack,其 Push 方法接受空值。

解决方案可以是使用完全限定的名称来引用您的自定义 Stack:

var item1 = new YourNamespace.Stack();
item1.Push(null);

我使用以下代码模拟了此问题:

using System.Collections;
namespace ConsoleApp3Classes {
    public class Stack {
        public readonly Stack<string> foodStack = new Stack<string>();
        public void Push(object obj) {
            if (obj is null) {
                throw new ArgumentNullException(nameof(obj), "不能向堆栈中添加空值");
            }
            string foodObject = obj.ToString();
            foodStack.Push(foodObject);
        }
    }
}
namespace ConsoleApp3 {
    internal class Program {
        static void Main(string[] args) {
            var item1 = new Stack(); item1.Push(null);
            var item1a = new ConsoleApp3Classes.Stack(); item1a.Push(null);
        }
    }
}

var item1 = new Stack(); item1.Push(null); 不会生成异常,而下一行会生成异常。

英文:

What's your environment/target?

// .NET FW 4.8, C# 5.0
using System;
using System.Collections.Generic;
namespace NsCustomStack {
	public class Astack {
		public readonly Stack&lt;string&gt; foodStack = new Stack&lt;string&gt;();
		public void Push(object obj) {
			if (obj == null) {
				throw new InvalidOperationException(&quot;You cannot add null to the stack&quot;);
			}
			string foodObject = obj.ToString();
			foodStack.Push(foodObject);
		}
	}
	class CustomStack {
		static void Main() {
			Astack item1 = new Astack(); item1.Push(null);
		}
	}
}

Result:

Unhandled Exception: System.InvalidOperationException: You cannot add null to the stack

___ at NsCustomStack.Astack.Push(Object obj)

___ at NsCustomStack.CustomStack.Main()

UPD My guess is

Stack item1 = new Stack();
item1.Push(null);

uses System.Collections.Stack which Push method accepts null.

The solution can be to use the fully qualified name for your custom Stack:

var item1 = new YourNamespace.Stack();
item1.Push(null);

I simulated the issue with this code:

using System.Collections;
namespace ConsoleApp3Classes {
    public class Stack {
        public readonly Stack&lt;string&gt; foodStack = new Stack&lt;string&gt;();
        public void Push(object obj) {
            if (obj is null) {
                throw new ArgumentNullException(nameof(obj), &quot;You cannot add null to the stack&quot;);
            }
            string foodObject = obj.ToString();
            foodStack.Push(foodObject);
        }
    }
}
namespace ConsoleApp3 {
    internal class Program {
        static void Main(string[] args) {
            var item1 = new Stack(); item1.Push(null);
            var item1a = new ConsoleApp3Classes.Stack(); item1a.Push(null);
        }
    }
}

"var item1 = new Stack(); item1.Push(null);" doesn't generate the exception while the next line does.

huangapple
  • 本文由 发表于 2023年7月3日 01:59:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600169.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定