捕获在不同线程中抛出的异常

编程入门 行业动态 更新时间:2024-10-20 20:33:00
本文介绍了捕获在不同线程中抛出的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的一个方法 (Method1) 产生一个新线程.该线程执行一个方法 (Method2) 并在执行期间抛出异常.我需要获取有关调用方法的异常信息 (Method1)

One of my method (Method1) spawns a new thread. That thread execute a method (Method2) and during exectution an exception is thrown. I need to get that exception information on the calling method (Method1)

有什么办法可以在 Method1 中捕获在 Method2 中抛出的异常?

Is there someway I can catch this exception in Method1 that is thrown in Method2?

推荐答案

在 .NET 4 及更高版本中,您可以使用 Task 类而不是创建新的线.然后,您可以使用任务对象上的 .Exceptions 属性获取异常.有两种方法可以做到:

In .NET 4 and above, you can use Task<T> class instead of creating new thread. Then you can get exceptions using .Exceptions property on your task object. There are 2 ways to do it:

  • 在一个单独的方法中://您在某个任务的线程 class Program { static void Main(string[] args) { Task<int> task = new Task<int>(Test); task.ContinueWith(ExceptionHandler, TaskContinuationOptions.OnlyOnFaulted); task.Start(); Console.ReadLine(); } static int Test() { throw new Exception(); } static void ExceptionHandler(Task<int> task) { var exception = task.Exception; Console.WriteLine(exception); } }

  • 在相同的方法中://您在调用者线程中处理异常

    class Program { static void Main(string[] args) { Task<int> task = new Task<int>(Test); task.Start(); try { task.Wait(); } catch (AggregateException ex) { Console.WriteLine(ex); } Console.ReadLine(); } static int Test() { throw new Exception(); } }

  • 注意你得到的异常是AggregateException.所有真正的异常都可以通过 ex.InnerExceptions 属性获得.

    Note that the exception which you get is AggregateException. All real exceptions are availible through ex.InnerExceptions property.

    在 .NET 3.5 中,您可以使用以下代码:

    In .NET 3.5 you can use the following code:

  • //您在子线程 class Program { static void Main(string[] args) { Exception exception = null; Thread thread = new Thread(() => SafeExecute(() => Test(0, 0), Handler)); thread.Start(); Console.ReadLine(); } private static void Handler(Exception exception) { Console.WriteLine(exception); } private static void SafeExecute(Action test, Action<Exception> handler) { try { test.Invoke(); } catch (Exception ex) { Handler(ex); } } static void Test(int a, int b) { throw new Exception(); } }

  • 或者//你在调用者线程中处理异常

    class Program { static void Main(string[] args) { Exception exception = null; Thread thread = new Thread(() => SafeExecute(() => Test(0, 0), out exception)); thread.Start(); thread.Join(); Console.WriteLine(exception); Console.ReadLine(); } private static void SafeExecute(Action test, out Exception exception) { exception = null; try { test.Invoke(); } catch (Exception ex) { exception = ex; } } static void Test(int a, int b) { throw new Exception(); } }

  • 更多推荐

    捕获在不同线程中抛出的异常

    本文发布于:2023-11-12 12:23:20,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1581486.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:线程   抛出   异常

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!