gpt4 book ai didi

c# - 当前上下文中不存在 ThrowException

转载 作者:行者123 更新时间:2023-11-30 21:17:09 25 4
gpt4 key购买 nike

在 vs2010 中做一些练习。

使用 try/catch/finally 并获得以下消息:

当前上下文中不存在抛出异常。我是否缺少使用 ThrowExcpetion 的 using 语句?

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

class Program
{
static string[] eTypes = { "none", "simple", "index", "nested index" };
static void Main(string[] args)
{
foreach (string eType in eTypes)
{
try
{
Console.WriteLine("Main() try");
Console.WriteLine("Throwing: \"{0}\") Called.",eType);

ThrowException(eType);
Console.WriteLine("Main() try continuing");

}
catch (System.IndexOutOfRangeException e)
{
Console.WriteLine("Main System.indexoutofrange catch",
e.Message);
}
finally
{
Console.WriteLine("Main finally");
}
Console.WriteLine();
}
Console.ReadKey();

}
}

最佳答案

您没有定义名为 ThrowException 的方法。

如果您试图引发异常,则不会这样做。你会这样做:

 throw new SomeTypeOfException(eType);

根据您的测试,我怀疑您想要:

 throw new IndexOutOfRangeException(eType);

有关详细信息,请参阅 throw (C# Reference) .

关于c# - 当前上下文中不存在 ThrowException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4940712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com