[remoting] Serialize exceptions between domains inside try/catch
[mono.git] / mono / tests / appdomain-serialize-exception.cs
1 using System;
2 using System.Reflection;
3 using System.Runtime.Serialization;
4
5 public class UnserializableException : Exception
6 {
7 }
8
9 public class TestOutput : MarshalByRefObject
10 {
11         public void ThrowUnserializable ()
12         {
13                 Console.WriteLine("Throwing Unserializable exception in AppDomain \"{0}\"", AppDomain.CurrentDomain.FriendlyName);
14                 throw new UnserializableException ();
15         }
16 }
17
18 public class Example
19 {
20         public static int Main ()
21         {
22                 string original_domain = AppDomain.CurrentDomain.FriendlyName;
23
24                 AppDomain ad = AppDomain.CreateDomain("subdomain");
25                 try {
26                         TestOutput remoteOutput = (TestOutput) ad.CreateInstanceAndUnwrap(
27                                 typeof (TestOutput).Assembly.FullName,
28                                 "TestOutput");
29                         remoteOutput.ThrowUnserializable ();
30                 } catch (SerializationException) {
31                         Console.WriteLine ("Caught serialization exception");
32                 } catch (Exception) {
33                         Console.WriteLine ("Caught other exception");
34                         Environment.Exit (1);
35                 } finally {
36                         Console.WriteLine ("Finally in domain {0}", AppDomain.CurrentDomain.FriendlyName);
37                         if (original_domain != AppDomain.CurrentDomain.FriendlyName)
38                                 Environment.Exit (2);
39                         AppDomain.Unload (ad);
40                 }
41
42                 Console.WriteLine ("All OK");
43                 return 0;
44         }
45 }