Tag: exception

无法捕获由PInvoke调用的C dll引起的exception

我正在编写一个使用最新MediaInfoLib Dll的C#.NET 3.5程序。 它似乎导致某些文件的exception。 我想捕获这些exception并确保我的程序继续运行, 但由于某种原因,我无法通过简单的try / catch语句来捕获它。 PInvoke方法: [DllImport(“MediaInfo.dll”)] private static extern IntPtr MediaInfo_New(); [DllImport(“MediaInfo.dll”)] private static extern IntPtr MediaInfo_Open(IntPtr Handle,MarshalAs(UnmanagedType.LPWStr)] string FileName); 用法: Handle = MediaInfo_New(); try{ MediaInfo_Open(Handle, FileName) } catch { } 调用MediaInfo_Open(Handle,FileName)可能会导致exception。 我的程序退出并且“vshost32-clr2.exe”崩溃,而不是使用try / catch语句捕获错误。 (它也作为发布版本崩溃,没有附加调试器) 在搜索网络后,我发现有人建议检查“启用非托管代码调试”,这只导致我的程序退出而没有vshost32-clr2.exe崩溃。 知道我怎么能抓住exception吗?

如何使用调试器处理任务中的exception?

我在MSDN上研究了这篇文章 ,以及有关此主题的SO的一些问题/答案,但无法理解为什么下面的代码不起作用(在示例控制台应用程序中)。 根据MSDN,预计会抛出AggregateException,它将包含一个带有hello消息的内部exception。 相反,这个helloexception未处理。 它发生在调试器内部。 如果按“继续”或“独立运行”,则按预期工作。 有没有办法避免在VS中一直按下继续? 毕竟, Try…Catch块中的任何内容都被认为是在单线程编程模型中处理的。 否则,调试可能是一场噩梦。 VB.NET Sub Main() Try Task.Factory.StartNew(AddressOf TaskThatThrowsException).Wait() Catch ex As AggregateException Console.WriteLine(ex.ToString) ‘does not get here until you hit Continue End Try End Sub Private Sub TaskThatThrowsException() Throw New Exception(“hello”) ‘exception was unhandled End Sub C# namespace ConsoleApplication1 { class Program { static void Main(string[] args) […]

Nunit测试给出结果OneTimeSetUp:找不到合适的构造函数

我有一个问题,NUnit告诉我:“找不到合适的构造函数”。 是什么导致这个? 我还得到另一条消息:“exception没有堆栈跟踪”。 这两条消息只是一遍又一遍地重复。 这是我的代码 [TestFixture] public class SecurityServiceTests { private IContext stubIContext; private ISecurityService securityService; private IWindsorContainer windsorContainer; public SecurityServiceTests(IContext stubIContext) { this.stubIContext= stubIContext; } [TestFixtureSetUp] public void TestSetup() { //Mocks the database context stubIContext= MockRepository.GenerateStub(); var returnedList = new List(); stubIContext.Stub(a => a.GetUserSecurities(null)).IgnoreArguments().Return(returnedList); securityService = new SecurityService(windsorContainer); } [Test] public void ControllerShouldGetUserGroupForCurrentUsers() { […]

PowerShell ParameterBindingException

我目前正在构建一个应用程序,以从ASP.NET MVC网站自动执行某些Exchange 2010操作。 现在,当我尝试调用New-AddressList命令时,我遇到了一个ParameterBindingException。 我正在尝试创建以下调用(有效): new-AddressList -Name “7 AL” -RecipientContainer “myDomain.local/Customers/7” -IncludedRecipients ‘AllRecipients’ -Container ‘\’ -DisplayName “7 AL” 我是通过以下方式做到的: var NewAddressList = new Command(“New-AddressList”); NewAddressList.Parameters.Add(“Name”, “7 AL”); NewAddressList.Parameters.Add(“RecipientContainer”, “myDomain.local/Customers/7”); NewAddressList.Parameters.Add(“IncludedRecipients”, “AllRecipients”); NewAddressList.Parameters.Add(“Container”, @”\”); NewAddressList.Parameters.Add(“DisplayName”, “7 AL”); CommandsList.Add(NewAddressList); 此命令列表提供给我调用的管道,给出以下错误: New-AddressList:输入对象不能绑定到命令的任何参数,因为该命令不接受管道输入或输入及其属性与接受管道输入的任何参数都不匹配。 CategoryInfo:InvalidArgument:(7:PSObject)[New-AddressList],ParameterBindingException FullyQualifiedErrorId:InputObjectNotBound,Microsoft.Exchange.Management.SystemConfigurationTasks.NewAddressList 任何可能导致这种情况的线索? 使用Trace-Command输出给出: PS C:\Users\ext_kefu> Trace-Command -Name parameterbinding -Expression {New-AddressList -Name “7 AL” -RecipientContainer “myDomain.local/Customers/7” […]

首先检查数据库单元是否为空

我通过C#访问MS Access 2007数据库,每当我尝试读取空单元格时,我都会收到exception。 具体来说,我正在尝试读取可能会或可能不会为空的“日期/时间”单元格。 我正在使用OLE DB ,并填充了一个DataSet。 这些条件都不起作用: DataSet dataSet = GetDataSet(); DataRow row = dataSet.Tables[0].Rows[0]; DateTime time = new DateTime(); time = (DateTime)row[5]; // Exception thrown 在尝试分配单元格之前如何检查单元格是否为空? 这些都不起作用: if(row[5] == null) ; if(row[5] == DBNull) ; if(row[5] == (String)””) ; 编辑:我应该提到:当我调试时,它说行[5]等于“System.DBNull”,但是当我尝试“if(row [5] == DBNULL)”时出现错误。 错误说“DBNULL是一种类型,在给定的上下文中无效”。

由客户端看不到相同类型的Web服务引发的自定义exception(使用共享程序集)

我创建了一个自定义exceptionInvalidSessionException 。 但是,当尝试捕获或评估引发的exception是否属于该类型时,它不起作用。 这意味着EX is和Catch Ex都不会评估为InvalidSessionException try { acc = this.fda.GetAccountHeader(this.selectedTicket.AccountId); } catch (Exception ex) { if (ex is Enterprise.Data.InformationModel.CustomExceptions.InvalidSessionException) { this.lblError.Text = Resources.Resource.error_sessionedTimedOut; this.MPError.Show(); } return; } 我也试过(结果没有任何差别) catch (Enterprise.Data.InformationModel.CustomExceptions.InvalidSessionException ex) { this.lblError.Text = Resources.Resource.error_sessionedTimedOut; this.MPError.Show(); return; } catch (Exception ex) { return; } 据我所知,抛出的exception是正确的类型。 更多信息: ex.GetType().FullName = “System.ServiceModel.FaultException1[[System.ServiceModel.ExceptionDetail, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]” 服务上是否启用了重用类型?

抛出exception后继续循环迭代

假设我有一个这样的代码: try { for (int i = 0; i < 10; i++) { if (i == 2 || i == 4) { throw new Exception("Test " + i); } } } catch (Exception ex) { errorLog.AppendLine(ex.Message); } 现在,显然执行将在i==2上停止,但我想让它完成整个迭代,以便在errorLog有两个条目(对于i==2和i==4 )所以,是否可能继续迭代甚至抛出exception?

获取第一次机会exception的行号

我从某个地方抛出了exception,但是我从编译器得到的只是“mscorlib.dll中出现类型’System.ArgumentOutOfRangeException’的第一次机会exception”。 这对我来说没用,因为那不是我的代码(很确定它是默认库)。 我想看到堆栈跟踪或其他什么,所以我知道在我的代码中出错的地方。 它是一个相当大的代码库(其中大部分都不是我的),而且还有很多multithreading和其他东西正在进行中,因此几乎不可能尝试逐步完成代码而不知道从哪里开始查找。 是否有某些设置可以使所有exception触发中断,因此我可以在调用堆栈出现时看到它们,而不是仅仅让它们在输出中以无效的错误消息无声地失败?

位图克隆问题

请考虑以下代码来加载,修改和保存位图图像: using (Bitmap bmp = new Bitmap(“C:\\test.jpg”)) { bmp.RotateFlip(RotateFlipType.Rotate180FlipNone); bmp.Save(“C:\\test.jpg”); } 它运行没有任何例外。 但考虑一下: using (Bitmap bmp = new Bitmap(“C:\\test.jpg”)) { using (Bitmap bmpClone = (Bitmap)bmp.Clone()) { //You can replace “bmpClone” in the following lines with “bmp”, //exception occurs anyway bmpClone.RotateFlip(RotateFlipType.Rotate180FlipNone); bmpClone.Save(“C:\\test.jpg”); } } 它以ExternalException结束,并显示以下消息:“GDI +中发生了一般错误”。 这有什么不对? 对打开的文件有什么样的锁定? 如果是这样,为什么第一个块有效? 克隆System.Drawing.Bitmap的正确代码是什么,而我们可能需要在内存中编辑主对象或其克隆,并且仍然将它们都加载到内存中?

如何处理抛出外部dll的exception?

我开发了一个使用外部DLL作为FTPServer的项目,我在我的项目中创建了这样的FTP服务器: private ClsFTPServer _ClsFTPServer; _ClsFTPServer = new ClsFTPServer(FTPUserName, FTPPassword, FTPPath); 上面的代码创建了一个FTP服务器类的实例,该类在它的构造函数上启动FTPserver,它作为一个模块独立工作,而客户端正确发送它们的请求,但是当一个不正确的请求到达FTP服务器时它抛出exception并导致我的崩溃申请。 如何处理外部dll抛出的exception以防止我的应用程序崩溃?