Tag: exception handling

是否可以尝试/捕获整个C#程序?

AC#程序由以下方式调用: Application.Run (new formClass ()); 我想对整个事情进行尝试/捕获以捕获任何未捕获的exception。 当我把它放在这个Run方法时,不会捕获exception; 当程序在未捕获的exception之后终止时,控制仅返回此处。 我在哪里可以使用try / catch来覆盖整个程序? 谢谢!

如何在C#中处理多播委托中的exception?

我已经获得了一些我通过多播委托调用的代码。 我想知道如何能够赶上并管理那里提出的任何exception,而目前还没有进行管理。 我无法修改给出的代码。 我一直在环顾四周,发现需要调用GetInvocationList(),但不确定这是否有用。

如何使用AsyncPump在异步控制台应用程序中保留exception上下文?

我正在使用Steven Toub优秀的AsyncPump类 ,它允许控制台应用程序使用async / await关键字。 但是,我遇到的问题是,代码中抛出的exception会被泵捕获然后重新抛出,这会导致原始调用堆栈和exception上下文丢失。 这是我的测试代码: class Program { static void Main(string[] arg) { AsyncPump.Run(() => MainAsync()); } static async Task MainAsync() { throw new Exception(); // code should break here } } 如果运行此测试,则调试器不会根据需要中断throw new Exception() 。 相反,它打破了t.GetAwaiter().GetResult() ,它是AsyncPump类本身的一部分。 这使调试应用程序非常困难。 有没有办法重新抛出exception,以便调试器在保留调用堆栈和上下文的同时在原始位置中断?

在任务中抛出exception – “等待”与等待()

static async void Main(string[] args) { Task t = new Task(() => { throw new Exception(); }); try { t.Start(); t.Wait(); } catch (AggregateException e) { // When waiting on the task, an AggregateException is thrown. } try { t.Start(); await t; } catch (Exception e) { // When awating on the task, the exception […]

如何获取Windowsapp store应用的标题和版本信息?

此代码在我的WP8应用程序中正常工作: void App_UnhandledException(object sender, UnhandledExceptionEventArgs args) { string appName; string appVersion; var xmlReaderSettings = new XmlReaderSettings { XmlResolver = new XmlXapResolver() }; using (var xmlReader = XmlReader.Create(“WMAppManifest.xml”, xmlReaderSettings)) { xmlReader.ReadToDescendant(“App”); appName = xmlReader.GetAttribute(“Title”); appVersion = xmlReader.GetAttribute(“Version”); } WAMS_EXCEPTIONLOG wamsel = new WAMS_EXCEPTIONLOG { appNameAndVersion = string.Format(“{0} {1}”, appName, appVersion), ExceptionMsg = args.ExceptionObject.Message, InnerException = args.ExceptionObject […]

我可以执行多个Catch块吗?

这有点抽象,但是有没有办法抛出exception并让它进入多个catch块? 例如,如果它匹配特定exception后跟非特定exception。 catch(Arithmetic exception) { //do stuff } catch(Exception exception) { //do stuff }

如何在使用HttpClient.GetAsync()时确定404响应状态

我试图确定HttpClient的GetAsync方法在使用C#和.NET 4.5的404错误的情况下返回的response 。 目前我只能说出错误已经发生而不是错误的状态,如404或超时。 目前我的代码我的代码如下所示: static void Main(string[] args) { dotest(“http://error.123”); Console.ReadLine(); } static async void dotest(string url) { HttpClient client = new HttpClient(); HttpResponseMessage response = new HttpResponseMessage(); try { response = await client.GetAsync(url); if (response.IsSuccessStatusCode) { Console.WriteLine(response.StatusCode.ToString()); } else { // problems handling here string msg = response.IsSuccessStatusCode.ToString(); throw new Exception(msg); } } […]

如何更好地理解“异步 – 处理多个exception”一文中的代码/语句?

运行以下C#控制台应用程序 class Program { static void Main(string[] args) { Tst(); Console.ReadLine(); } async static Task Tst() { try { await Task.Factory.StartNew (() => { Task.Factory.StartNew (() => { throw new NullReferenceException(); } , TaskCreationOptions.AttachedToParent ); Task.Factory.StartNew ( () => { throw new ArgumentException(); } ,TaskCreationOptions.AttachedToParent ); } ); } catch (AggregateException ex) { // this […]

为什么这个方法返回double.PositiveInfinity不是DivideByZeroException?

我在VS2015 C#interactive中运行了以下片段并得到了一些非常奇怪的行为。 > double divide(double a, double b) . { . try . { . return a / b; . } . catch (DivideByZeroException exception) . { . throw new ArgumentException(“Argument b must be non zero.”, exception); . } . } > divide(3,0) Infinity > 3 / 0 (1,1): error CS0020: Division by constant zero […]

评估需要一个线程暂时运行。 使用“监视”窗口执行评估

我完全卡住了 。 我正在测试MetaTrader API并在尝试在VS 2010的立即窗口中运行方法时获取下一个错误: mscorlib.dll中出现’System.Threading.ThreadAbortException’类型的第一次机会exception System.Runtime.Remoting.dll中出现’System.Threading.ThreadAbortException’类型的第一次机会exception 评估需要一个线程暂时运行。 使用“监视”窗口执行评估。 这是什么意思? 是否会因运行时版本的差异而发生(api 2.0,app 4.0)?