草庐IT

异常系列

全部标签

c# - 为什么 List<T> 出现 "Index was out of range"异常而不是数组?

当我初始化数组并使用索引器访问元素时,效果很好:object[]temp=newobject[5];temp[0]="bar";现在我希望同样适用于List,假设您可以通过将容量传递给构造函数来初始化它:Listtemp=newList(5);temp[0]="bar";然而,最后一行抛出以下异常:Indexwasoutofrange.Mustbenon-negativeandlessthanthesizeofthecollection为什么List会发生这种情况类型,但不是数组?由于数组只是CLR集合的较低级别抽象,那么为什么会出现此异常?原创question通过AwaisMahmo

c# - GetAuthorizationGroups() 抛出异常

PrincipalContextcontext=newPrincipalContext(ContextType.Domain,"ipofmachine","DC=xyz,DC=org","username","Password");UserPrincipaluserPrinciple=UserPrincipal.FindByIdentity(context,"User0");vargroups=userPrinciple.GetAuthorizationGroups();if(userPrinciple!=null){foreach(GroupPrincipalgpingroups){

c# - MSBuild 未处理的异常 : The FileName property should not be a directory unless UseShellExecute is set

版本dotnet核心SDK:2.1.403docker:18.09.7Linux内核:5.0.0-27Ubuntu:18.04.3问题我正在docker中运行一个ASP.NETCore项目。当我docker-composeup时,我得到以下信息:UnhandledException:Microsoft.Build.BackEnd.NodeFailedToLaunchException:TheFileNamepropertyshouldnotbeadirectoryunlessUseShellExecuteisset.--->System.ComponentModel.Win32Exce

c# - 忽略 xUnit.net 中的异常

在某些情况下,我不关心抛出什么异常(只要抛出一些异常即可)。不幸的是,Assert.Throws(someDelegate);除非恰好抛出Exception的实例(因此不是派生类的实例),否则不会通过。我知道我可以获得我想要的行为Exceptionexception=Record.Exception(someDelegate);Assert.NotNull(exception);但它读起来不对。我是否在xUnit中遗漏了一些具有我想要的行为的东西?这里有两个测试表明我的意思:[Fact]publicvoidThrows_exception_and_passes(){Exceptione

c# - 被遗弃的互斥异常

我第一次尝试使用互斥锁,并在程序的两个独立实例上执行以下代码publicvoidasynchronousCode(){using(varmutex=newMutex(false,"mySpecialMutex")){if(!mutex.WaitOne(1000,false)){Console.WriteLine("Firstcheck-someonelockedthemutex");}if(!mutex.WaitOne(3000,false)){Console.WriteLine("Secondcheck-someonelockedthemutex");}else{Console.Wr

c# - 我怎样才能抛出带有特定 HResult 的异常?

我想测试下面的代码:privateboolTestException(Exceptionex){if((Marshal.GetHRForException(ex)&0xFFFF)==0x4005){returntrue;}returnfalse;}我想以某种方式设置Exception对象以返回正确的HResult,但我在Exception类中看不到允许这样做的字段。我该怎么做? 最佳答案 我找到了三种方法:使用System.Runtime.InteropServices.ExternalException类,将错误代码作为参数传入:

c# - 接口(interface)不能被实例化,但这是一个异常吗

这个问题在这里已经有了答案:HowdoestheC#compilerdetectCOMtypes?(4个答案)关闭8年前。看到我实际上必须实例化一个接口(interface)才能在C#中使用WordInteroop后,我感到非常惊讶。Microsoft.Office.Interop.Word.Applicationword=newMicrosoft.Office.Interop.Word.Application();根据其XML文档所说的Microsoft.Office.Interop.Word.Application是一个接口(interface):怎么可能,visualstudio

c# - iTextSharp 异常 : PDF header signature not found

我正在使用iTextSharp阅读PDF文档的内容:PdfReaderreader=newPdfReader(pdfPath);using(StringWriteroutput=newStringWriter()){for(inti=1;i99%它工作正常。然而,有一个PDF文件有时会抛出这个异常:PDFheadersignaturenotfound.StackTrace:atiTextSharp.text.pdf.PRTokeniser.CheckPdfHeader()atiTextSharp.text.pdf.PdfReader.ReadPdf()atiTextSharp.text

c# - Task.Run 和 Task.Factory.StartNew 之间不同的异常处理

我在使用Task.Factory.StartNew并trycatch抛出的exception时遇到问题。在我的应用程序中,我有一个长时间运行的任务,我想将其封装在Task.Factory.StartNew(..,TaskCreationOptions.LongRunning);但是,当我使用Task.Factory.StartNew时,异常没有被捕获。然而,当我使用Task.Run时,它的工作方式与我预期的一样,我认为它只是Task.Factory.StartNew的包装器(根据例如thisMSDNarticle).此处提供了一个工作示例,不同之处在于使用Task.Run时将异常写入控

c# - WebAPI 全局异常处理

我有一个ASPWebAPI项目。我正在尝试在我的基本Controller上设置一个全局异常处理程序。所以我像这样创建了一个ExceptionFilterAttribute。usingSystem.Web.Http.Filters;publicclassMyExceptionFilterAttribute:ExceptionFilterAttribute{protectedstaticreadonlyILoglog=LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);publicoverridevoidOnE