1 using System;
2 using System.Threading;
3
4 public class FooException : Exception
5 {
6 public FooException(const string& message_) : base(message_)
7 {
8 }
9 }
10
11 void foo()
12 {
13 throw FooException("foo");
14 }
15
16 class ThreadData
17 {
18 public void SetCapturedException(const ExceptionPtr& exception_)
19 {
20 exception = exception_;
21 }
22 public const ExceptionPtr& GetCapturedException() const
23 {
24 return exception;
25 }
26 private ExceptionPtr exception;
27 }
28
29 void ThreadFunction(void* arg)
30 {
31 ThreadData* threadData = cast<ThreadData*>(arg);
32 try
33 {
34 foo();
35 }
36 catch (const Exception& ex)
37 {
38 ExceptionPtr exceptionPtr = CaptureCurrentException();
39 threadData->SetCapturedException(exceptionPtr);
40 }
41 }
42
43 void main()
44 {
45 try
46 {
47 ThreadData threadData;
48 Thread thread = Thread.StartFunction(ThreadFunction, &threadData);
49 thread.Join();
50 ExceptionPtr capturedExceptionPtr = threadData.GetCapturedException();
51 if (capturedExceptionPtr.Exception() != null)
52 {
53 ThrowCapturedException(capturedExceptionPtr);
54 }
55 }
56 catch (const Exception& ex)
57 {
58 Console.Error() << ex.ToString() << endl();
59 }
60 }