Tag: exception

catchedexception如何为null(不是NullReferenceException)?

我遇到了一个相当奇怪的小问题。 在下面的代码中,我无法理解e如何为null ; try { //Some Code here } catch (Exception e) { //Here e is null } 据我所知, throw null将被转换为throw new NullReferenceException() 。 问题似乎与multithreading有关,因为删除另一个线程似乎也解决了它。 或者至少我只在上面的代码在新线程中运行时才看到这个。 整个程序使用很multithreading,有点复杂。 无论如何我的问题是,如何才能为空? – 希望答案可以帮助找到这个问题的根源。 编辑我发现它,因为它在catch语句中导致了NullReferenceException,并且使用调试器我看到同样的事情。 编辑2第二天打开VisualStudio再次尝试,没有代码更改,现在相同的口号被“调用”,但这次e不为空。 看起来这是一个VS故障。

System.Drawing.Icon构造函数抛出“操作成功完成”exception

在Windows XP计算机上,以下代码抛出System.ComponentModel.Win32Exception,并显示消息“操作已成功完成” System.Drawing.Icon icon = new System.Drawing.Icon(“icon.ico”); 我可以阻止程序崩溃 try { System.Drawing.Icon icon = new System.Drawing.Icon(“icon.ico”); } catch(System.ComponentModel.Win32Exception ex) { if (ex.NativeErrorCode != 0) { throw; } } 但当然没有设置图标。 完整的堆栈跟踪是 at System.Drawing.Icon.Initialize(Int32 width, Int32 height) at System.Drawing.Icon..ctor(String fileName, Int32 width, Int32 height) at System.Drawing.Icon..ctor(String fileName) at hermes.Window1..ctor() in D:\\projects\\hermesclient\\hermesWPF\\hermes\\Window1.xaml.cs:line 50″ 第50行是我发布的原始行。 这是一个WPF应用程序,在Windows 7机器上,代码工作正常。 编辑:原来这个图标在Windows XP中根本不起作用,添加256色版本似乎已经修复了它。

Visual Studio – 禁止某些“Exception thrown”消息

您可以在输出中隐藏某些方法的“Exception thrown”消息吗? 我使用HttpWebRequest进行服务器通信。 我会定期检查服务器是否可用(每秒几次)。 当服务器无法访问时,HttpWebRequest会抛出exception。 我抓住它并将GUI元素设置为false。 问题是当服务器无法访问时,输出窗口会被“Exception thrown”消息弄得乱七八糟。 我知道您可以右键单击输出窗口并取消选中“exception消息”。 但我不仅是一个从事该项目的人,而且可能还有人希望看到其他一些exception消息。 我需要的例子: // Keep showing “Exception thrown” message. static void Foo() { try { throw new Exception(); } catch (Exception e) { // Process exception } } // Suppress “Exception thrown” message in this method. static void FooSuppress() { try { throw new Exception(); } catch […]

得到错误“位置0没有行”

请帮我解决这个问题…我收到的错误是“0号位没有行”,“索引超出范围exception是用户代码无法解决的” 以下是我的代码 protected void Page_Load(object sender, EventArgs e) { MTMSService obj = new MTMSService(); DBAccess db = new DBAccess(); { MTMSDTO objc = new MTMSDTO(); { objc.TaskID = Convert.ToInt32(Session[“TaskID”]); DataSet rep = obj.GetReports(objc); DataView Rprts = new DataView(); Rprts.Table = rep.Tables[0]; LblTaskID.Text = rep.Tables[0].Rows[0][“TaskID”].ToString(); LblTaskName.Text = rep.Tables[1].Rows[0][“TaskName”].ToString(); LblDueDate.Text = rep.Tables[2].Rows[0][“DueDate”].ToString(); LblDescription.Text = rep.Tables[3].Rows[0][“Description”].ToString(); LblAssignBy.Text = […]

你能让ReSharper在Intellisense中显示exception吗?

我是ReSharper提供的被重写的Intellisense行为的粉丝。 但是,我似乎无法显示exception(任何带有/// xml注释的内容)。 有谁知道启用此function的方法? 注意:这不是与不包括来自dll的xml注释文件有关的问题,因为它发生在所有类(甚至是核心的.Net文件)中。 另外,我确保包含XML文件的任何我自己的库也没有在注释中显示exception。 注意2:我尝试选择Visual Studio Intellisense而不是ReSharper Intellisense(通过Tools > Options > Text Editor > C#并打开Statement completion部分下的Auto list members和Parameter information设置)。 这似乎带来了exception(在Visual Studio重启后)。 但是,它仅在我通过制表符完成时调出Intellisense时才起作用,而不是当我通过将鼠标hover在方法调用上时调出一个方法的工具提示(这曾经工作……它甚至可以在启动后的前几秒正常工作Visual Studio,但然后再次禁用它)。 眼镜: ReSharper 5.1.3 Visual Studio 2008专业版

如何在任务抛出exception而不等待终结器时使应用程序崩溃

我们在.Net 4(没有异步等待可用)应用程序中使用任务,有时它们用于启动“Fire and Forget”操作,如下所示: private void Test() { Task task = Task.Factory.StartNew(() => { throw new ApplicationException(“Test”); }); } 我们希望此exception在不等待任务的情况下使应用程序崩溃(否则将其置于任务中是没有意义的,至少在我们的场景中)并且不等待终结器,因为我们想要在发生意外错误时关闭应用程序避免状态损坏(我们正在保存exception发生时的状态)。 我的猜测是,不知怎的,我们应该使用延续任务但是将延续代码放在另一个不会使应用程序崩溃的任务中,所以我在这里被阻止了。 任何帮助将非常感谢 编辑:如果切换到ThreadPool,结果是预期的结果。 以下代码崩溃了应用程序: ThreadPool.QueueUserWorkItem((c) => { throw new ApplicationException(“Test”); });

使用AvalonDock 2.0时未处理的’System.ComponentModel.Win32Exception’

我正在使用AvalonDock 2.0 ,当我打开一个dock容器时,在调试模式下应用程序崩溃(它在没有调试的情况下运行时工作正常)。 我得到以下exception: WindowsBase.dll中出现未处理的“System.ComponentModel.Win32Exception”类型exception 附加信息:操作成功完成 我遇到了这个答案 ,建议取消选中“例外设置”中的框。 有线的事情是它第一次使用它。 但它不再存在了。 我试过其他机器也不行。 任何有关如何解决此问题的建议。 Avalon代码(第5行引发的exception) protected override IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == Win32Helper.WM_WINDOWPOSCHANGING) { if (_internalHost_ContentRendered) { // the below line throw the exception Win32Helper.SetWindowPos(_internalHwndSource.Handle, Win32Helper.HWND_TOP, 0, 0, 0, 0, Win32Helper.SetWindowPosFlags.IgnoreMove | Win32Helper.SetWindowPosFlags.IgnoreResize); } } return […]

使用WinDbg从minidump确定InnerException的行号

我正在尝试从转储中追踪NullReferenceException。 NullReferenceException不是崩溃exception,而是崩溃exception是TargetInvocationException,其InnerException是NullReferenceException。 我正在使用带有SOS的Windbg,我使用命令analyze -v ,这给了我NullReferenceException的调用堆栈: EXCEPTION_OBJECT: !pe f6cb150 Exception object: 000000000f6cb150 Exception type: System.NullReferenceException Message: Object reference not set to an instance of an object. InnerException: StackTrace (generated): SP IP Function 000000002CD9D8C0 000007FF01E7C639 MyDll!DoSomething2()+0xe99 000000002CD9DBE0 000007FF01E7B11D MyDll!DoSomething1()+0x43d 000000002CD9DD20 000007FF01E7AB11 MyDll!WorkerDoWork(System.Object, System.ComponentModel.DoWorkEventArgs)+0x51 000000002CD9DD80 000007FEEA68A0F2 System_ni!System.ComponentModel.BackgroundWorker.WorkerThreadStart(System.Object)+0x62 请注意,我得到了具有字节偏移的方法名称,但没有行号。 DoSomething2是一个很大的函数,所以NullReferenceException发生的地方并不明显。 我试图按照Tess Ferrandez博客中的说明操作: .Netexception – 跟踪代码中发生exception的位置 但是我很早就陷入困境,我尝试使用DoSomething2:7FF01E7C639的IP来确定方法DoSomething2的方法描述符!ip2md: > !ip2md 7FF01E7C639 […]

用于包装抛出API的.NET迭代器

我有一个带有API的类,它允许我询问对象,直到它抛出IndexOutOfBoundsException 。 我想将它包装到迭代器中,以便能够编写更清晰的代码。 但是,我需要捕获exception以停止迭代: static IEnumerable Iterator( ExAPI api ) { try { for( int i = 0; true; ++i ) { yield return api[i]; // will throw eventually } } catch( IndexOutOfBoundsException ) { // expected: end of iteration. } } 但… 与expression一起使用时,yield return语句不能出现在catch块或具有一个或多个catch子句的try块中。 有关更多信息,请参阅exception处理语句(C#参考).Statements(C#参考)。 (来自msdn ) 我怎么还能包装这个api?

尝试重新启动线程时发生ThreadStateException

尝试重新启动线程时,我不时会收到System.Threading.ThreadStateException。 有问题的代码如下: // Make sure the thread is done stopping while (this.mThread.ThreadState == ThreadState.Running) { Thread.Sleep(0); } // Respawn a thread if the current one is stopped or doesn’t exist if (this.mThread == null || this.mThread.ThreadState == ThreadState.Stopped) { this.mThread = new Thread(new ParameterizedThreadStart(Monitor)); } // Start the thread if (check) { this.mThread.Start(60000); } else […]