Tag: exception handling

如何尝试/捕获所有exception

我正在完成由其他人启动的UWP应用程序。 该应用程序经常崩溃,我总是在App.gics中结束 if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break(); 然后,我必须说“不,不要启动调试器”并关闭2个窗口。 有什么地方我可以放一个大的尝试/捕获,以便我不必每次发生时重新启动应用程序? 我在AppShell或App找不到任何东西。 或者我是否必须在每个事件处理程序中放置一个try / catch?

应该测试exception消息

有没有办法用shouldly测试exception消息? 一个例子: public class MyException: Exception{ } 要测试的方法: public class ClassUnderTest { public void DoSomething() { throw new MyException(“Message”); } } 我通常会以这种方式测试它: [TestMethod] public void Test() { try { new ClassUnderTest().DoSomething(); Assert.Fail(“Exception not thrown”); } catch(MyException me) { Assert.AreEqual(“Message”, me.Message); }catch(Exception e) Assert.Fail(“Wrong exception thrown”); } } 我应该现在可以测试是否抛出exception: [TestMethod] public void TestWithShouldly() { Should.ThrowException(() => […]

使用HttpWebRequest和StreamReader下载网页时出现未处理的exception

这是一个使用.NET 4.0的C#应用​​程序。 背景: 我一直在尝试优化从多个Web服务器下载文件的应用程序的一部分。 除了偶尔出现远程计算机关闭连接的情况外,一切工作都很顺利。 我认为这可能是由于各种因素造成的,包括网络问题,电源问题等。我想在连接关闭的情况下跳过下载。 但是,目前该应用程序会导致AppDomain.CurrentDomain.UnhandledException处理程序获取exception。 堆栈跟踪如下: 堆栈跟踪: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.IO.StreamReader.ReadBuffer() at System.IO.StreamReader.ReadToEnd() at ProjectName.ClassNetwork.DownloadFile(String _IP, Int32 _Port, String _File) at ProjectName.FormMain.GetIFile(ClassSensor Sensor, String& RawHolder, String[] FileData) at ProjectName.FormMain.GetLegacyData(ClassSensor Sensor) […]

GetWindowText()抛出错误而不被try / catch捕获

当我为GetWindowText运行下面的代码时,我得到以下错误作为内部exception抛出: {“尝试读取或写入受保护的内存。这通常表示其他内存已损坏。”} [DllImport(“user32.dll”, EntryPoint = “GetWindowTextLength”, SetLastError = true)] internal static extern int GetWindowTextLength(IntPtr hwnd); [DllImport(“user32.dll”, EntryPoint = “GetWindowText”, SetLastError = true)] internal static extern int GetWindowText(IntPtr hwnd, ref StringBuilder wndTxt, int MaxCount); try{ int strLength = NativeMethods.GetWindowTextLength(wndHandle); var wndStr = new StringBuilder(strLength); GetWindowText(wndHandle, ref wndStr, wndStr.Capacity); } catch(Exception e){ LogError(e) } 我有两个问题: 为什么错误没有被try catch捕获? […]

如何在不同的IOExceptions之间以编程方式区分?

我正在为写入Process对象的StandardInput流的代码执行一些exception处理。 进程有点像unix head命令; 它只读取部分输入流。 当进程终止时,写入线程失败: IOException The pipe has been ended. (Exception from HRESULT: 0x8007006D) 我想捕获这个exception,让它优雅地失败,因为这是预期的行为。 但是,对我而言,如何将其与其他IOExceptions进行强有力的区分并不明显。 我可以使用消息,但我理解这些是本地化的,因此这可能不适用于所有平台。 我也可以使用HRESULT,但我找不到任何指定此HRESULT仅适用于此特定错误的文档。 这样做的最佳方式是什么?

抛出VS重新抛出:同样的结果?

在网上引用了很多文档,特别是在SO上,例如: 在C#中重新抛出exception的正确方法是什么? “扔e”之间应该有区别 和“扔;”。 但是,来自: http : //bartdesmet.net/blogs/bart/archive/2006/03/12/3815.aspx , 这段代码: using System; class Ex { public static void Main() { // // First test rethrowing the caught exception variable. // Console.WriteLine(“First test”); try { ThrowWithVariable(); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } // // Second test performing a blind rethrow. // Console.WriteLine(“Second test”); try { […]

优雅地处理任务取消

当我需要能够取消大型/长期运行工作负载的任务时,我经常使用与此类似的模板来执行任务: public void DoWork(CancellationToken cancelToken) { try { //do work cancelToken.ThrowIfCancellationRequested(); //more work } catch (OperationCanceledException) { throw; } catch (Exception ex) { Log.Exception(ex); throw; } } 不应将OperationCanceledException记录为错误,但如果任务要转换为已取消状态,则不得吞下。 除了此方法的范围之外,不需要处理任何其他exception。 这总觉得有些笨重,默认情况下,visual studio会在OperationCanceledException中抛出(虽然因为我使用了这种模式,我现在因为OperationCanceledException而关闭了User-unhandled’)。 理想情况下,我认为我希望能够做到这样的事情: public void DoWork(CancellationToken cancelToken) { try { //do work cancelToken.ThrowIfCancellationRequested(); //more work } catch (Exception ex) exclude (OperationCanceledException) { Log.Exception(ex); throw; } } […]

在Quartz.net中处理JobExecutionException

可能是一个愚蠢的问题……但无论如何…… 我已经设置了quartz,并且可以安排作业,我可以确认作业(实现IJob接口)正在工作。 查看网站上的文档(教程的第3课): 允许从execute方法抛出的唯一exception类型是JobExecutionException 。 我希望当发生一个我没有显式处理的exception时,它应该抛出一个JobExecutionException,这样我就可以在’parent’应用程序中记录它。 我已将我的代码包装在try catch中,并抛出JobExecutionException,但现在在哪里处理它? 我没有在任何地方调用execute方法,这是由Quartz处理的(在一个单独的线程上)。 那么,如何在错误发生时处理该错误。 我真的不想吞下Job中的错误

CIL“错误”条款与C#中的“catch”条款有何不同?

根据CLI标准 (Partition IIA,第19章)和System.Reflection.ExceptionHandlingClauseOptions枚举的MSDN参考页面,有四种不同的exception处理程序块: catch子句: “捕获指定类型的所有对象。” filter子句: “仅在filter成功时输入处理程序”。 最后条款: “处理所有exception和正常退出。” fault子句: “处理所有exception,但不能正常退出。” 鉴于这些简短的解释(引自CLI标准,顺便说一句),这些应该映射到C#,如下所示: catch – catch (FooException) { … } filter – 在C#中不可用(但在VB.NET中为Catch FooException When booleanExpression ) 终于 – finally { … } 错误 – catch { … } 实验: 一个简单的实验表明,这种映射不是.NET的C#编译器真正做的事情: // using System.Linq; // using System.Reflection; static bool IsCatchWithoutTypeSpecificationEmittedAsFaultClause() { try { return MethodBase .GetCurrentMethod() […]

多个表单的exception处理

我正在看到不同的行为,在我调试时遇到exception被捕获或者没有被捕获,而我正在运行已编译的.exe。 我有两种forms(Form1和Form2)。 Form1上有一个按钮,它在Form2上实例化并调用ShowDialog。 Form2上有一个按钮,故意产生除零误差。 当我调试时,Form1中的catch块被命中。 当我运行已编译的.exe时,它没有被命中,而是我得到一个消息框,指出“你的应用程序中发生了未处理的exception。如果你点击继续,应用程序将忽略此错误并尝试继续。如果你单击退出,应用程序将立即关闭…尝试除以零“。 我的问题是为什么在调试时和运行.exe时会出现不同的行为? 如果这是预期的行为,那么是否有必要在每个事件处理程序中放置try / catch块? 这似乎有点疯狂,不是吗? 这是Form1的代码。 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { Form2 f2 = new Form2(); f2.ShowDialog(); } catch(Exception eX) { MessageBox.Show( eX.ToString()); //This line hit when debugging only } } } 这是Form2的代码: […]