捕获在不同线程中抛出的异常 [英] catch exception that is thrown in different thread

查看:31
本文介绍了捕获在不同线程中抛出的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个方法 (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:

  1. 在一个单独的方法中://您在某个任务的线程

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:

    1. //您在线程

    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();
        }
    }
    

  • 这篇关于捕获在不同线程中抛出的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆