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

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

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("您不能将空对象添加到堆栈");
}

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

}

主要方法:

  1. Stack item1 = new Stack();
  2. 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.

  1. public class Stack
  2. {
  3. public readonly Stack<string> foodStack = new Stack<string>();
  4. public void Push(object obj)
  5. {
  6. if (obj == null)
  7. {
  8. throw new InvalidOperationException("You cannot add null to the stack");
  9. }
  10. string foodObject = obj.ToString();
  11. foodStack.Push(foodObject);
  12. }

Main Method:

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

I have tried changing the type of exception.

答案1

得分: 1

你的环境/目标是什么?

  1. // .NET FW 4.8, C# 5.0
  2. using System;
  3. using System.Collections.Generic;
  4. namespace NsCustomStack {
  5. public class Astack {
  6. public readonly Stack<string> foodStack = new Stack<string>();
  7. public void Push(object obj) {
  8. if (obj == null) {
  9. throw new InvalidOperationException("You cannot add null to the stack");
  10. }
  11. string foodObject = obj.ToString();
  12. foodStack.Push(foodObject);
  13. }
  14. }
  15. class CustomStack {
  16. static void Main() {
  17. Astack item1 = new Astack(); item1.Push(null);
  18. }
  19. }
  20. }

结果:

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

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

___ 在 NsCustomStack.CustomStack.Main() 中

更新 我的猜测是

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

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

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

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

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

  1. using System.Collections;
  2. namespace ConsoleApp3Classes {
  3. public class Stack {
  4. public readonly Stack<string> foodStack = new Stack<string>();
  5. public void Push(object obj) {
  6. if (obj is null) {
  7. throw new ArgumentNullException(nameof(obj), "不能向堆栈中添加空值");
  8. }
  9. string foodObject = obj.ToString();
  10. foodStack.Push(foodObject);
  11. }
  12. }
  13. }
  14. namespace ConsoleApp3 {
  15. internal class Program {
  16. static void Main(string[] args) {
  17. var item1 = new Stack(); item1.Push(null);
  18. var item1a = new ConsoleApp3Classes.Stack(); item1a.Push(null);
  19. }
  20. }
  21. }

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

英文:

What's your environment/target?

  1. // .NET FW 4.8, C# 5.0
  2. using System;
  3. using System.Collections.Generic;
  4. namespace NsCustomStack {
  5. public class Astack {
  6. public readonly Stack&lt;string&gt; foodStack = new Stack&lt;string&gt;();
  7. public void Push(object obj) {
  8. if (obj == null) {
  9. throw new InvalidOperationException(&quot;You cannot add null to the stack&quot;);
  10. }
  11. string foodObject = obj.ToString();
  12. foodStack.Push(foodObject);
  13. }
  14. }
  15. class CustomStack {
  16. static void Main() {
  17. Astack item1 = new Astack(); item1.Push(null);
  18. }
  19. }
  20. }

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

  1. Stack item1 = new Stack();
  2. 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:

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

I simulated the issue with this code:

  1. using System.Collections;
  2. namespace ConsoleApp3Classes {
  3. public class Stack {
  4. public readonly Stack&lt;string&gt; foodStack = new Stack&lt;string&gt;();
  5. public void Push(object obj) {
  6. if (obj is null) {
  7. throw new ArgumentNullException(nameof(obj), &quot;You cannot add null to the stack&quot;);
  8. }
  9. string foodObject = obj.ToString();
  10. foodStack.Push(foodObject);
  11. }
  12. }
  13. }
  14. namespace ConsoleApp3 {
  15. internal class Program {
  16. static void Main(string[] args) {
  17. var item1 = new Stack(); item1.Push(null);
  18. var item1a = new ConsoleApp3Classes.Stack(); item1a.Push(null);
  19. }
  20. }
  21. }

"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:

确定