Sil*_*ent 106 c# multithreading exception-handling
我的一个方法(Method1)产生一个新线程.该线程执行一个方法(Method2),并在exectution期间抛出异常.我需要获取有关调用方法的异常信息(Method1)
在某种程度上,我可以捕获这个Method1被抛出的异常Method2吗?
oxi*_*min 176
在.NET 4及更高版本中,您可以使用Task<T>类而不是创建新线程.然后,您可以使用.Exceptions任务对象上的属性获取异常.有两种方法可以做到:
在一个单独的方法中://您在某个任务的线程中处理异常
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);
}
}
Run Code Online (Sandbox Code Playgroud)在同一方法中://您在调用者的线程中处理异常
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();
}
}
Run Code Online (Sandbox Code Playgroud)请注意,您获得的例外是AggregateException.所有真正的例外都可通过ex.InnerExceptions财产获得.
在.NET 3.5中,您可以使用以下代码:
//您在子线程中处理异常
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();
}
}
Run Code Online (Sandbox Code Playgroud)或者//您在调用者的线程中处理异常
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();
}
}
Run Code Online (Sandbox Code Playgroud)