|
3 | 3 | using System.IO; |
4 | 4 | using System.Runtime.Serialization; |
5 | 5 | using System.Runtime.Serialization.Formatters.Soap; |
6 | | -using System.Security.Permissions; |
7 | | - |
8 | | - // Define a serializable derived exception class. |
9 | | - [Serializable()] |
10 | | - class SecondLevelException : Exception, ISerializable |
11 | | - { |
12 | | - // This public constructor is used by class instantiators. |
13 | | - public SecondLevelException( string message, Exception inner ) : |
14 | | - base( message, inner ) |
15 | | - { |
16 | | - HelpLink = "http://MSDN.Microsoft.com"; |
17 | | - Source = "Exception_Class_Samples"; |
18 | | - } |
19 | | - |
20 | | - // This protected constructor is used for deserialization. |
21 | | - protected SecondLevelException( SerializationInfo info, |
22 | | - StreamingContext context ) : |
23 | | - base( info, context ) |
24 | | - { } |
25 | | - |
26 | | - // GetObjectData performs a custom serialization. |
27 | | - [SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter=true)] |
28 | | - public override void GetObjectData( SerializationInfo info, |
29 | | - StreamingContext context ) |
30 | | - { |
31 | | - // Change the case of two properties, and then use the |
32 | | - // method of the base class. |
33 | | - HelpLink = HelpLink.ToLower( ); |
34 | | - Source = Source.ToUpperInvariant(); |
35 | | - |
36 | | - base.GetObjectData( info, context ); |
37 | | - } |
38 | | - } |
39 | | - |
40 | | - class SerializationDemo |
41 | | - { |
42 | | - public static void Main() |
43 | | - { |
44 | | - Console.WriteLine( |
45 | | - "This example of the Exception constructor " + |
46 | | - "and Exception.GetObjectData\nwith Serialization" + |
47 | | - "Info and StreamingContext parameters " + |
48 | | - "generates \nthe following output.\n" ); |
49 | | - |
50 | | - try |
51 | | - { |
52 | | - // This code forces a division by 0 and catches the |
53 | | - // resulting exception. |
54 | | - try |
55 | | - { |
56 | | - int zero = 0; |
57 | | - int ecks = 1 / zero; |
58 | | - } |
59 | | - catch( Exception ex ) |
60 | | - { |
61 | | - // Create a new exception to throw again. |
62 | | - SecondLevelException newExcept = |
63 | | - new SecondLevelException( |
64 | | - "Forced a division by 0 and threw " + |
65 | | - "another exception.", ex ); |
66 | | - |
67 | | - Console.WriteLine( |
68 | | - "Forced a division by 0, caught the " + |
69 | | - "resulting exception, \n" + |
70 | | - "and created a derived exception:\n" ); |
71 | | - Console.WriteLine( "HelpLink: {0}", |
72 | | - newExcept.HelpLink ); |
73 | | - Console.WriteLine( "Source: {0}", |
74 | | - newExcept.Source ); |
75 | | - |
76 | | - // This FileStream is used for the serialization. |
77 | | - FileStream stream = |
78 | | - new FileStream( "NewException.dat", |
79 | | - FileMode.Create ); |
80 | | - |
81 | | - try |
82 | | - { |
83 | | - // Serialize the derived exception. |
84 | | - SoapFormatter formatter = |
85 | | - new SoapFormatter( null, |
86 | | - new StreamingContext( |
87 | | - StreamingContextStates.File ) ); |
88 | | - formatter.Serialize( stream, newExcept ); |
89 | | - |
90 | | - // Rewind the stream and deserialize the |
91 | | - // exception. |
92 | | - stream.Position = 0; |
93 | | - SecondLevelException deserExcept = |
94 | | - (SecondLevelException) |
95 | | - formatter.Deserialize( stream ); |
96 | | - |
97 | | - Console.WriteLine( |
98 | | - "\nSerialized the exception, and then " + |
99 | | - "deserialized the resulting stream " + |
100 | | - "into a \nnew exception. " + |
101 | | - "The deserialization changed the case " + |
102 | | - "of certain properties:\n" ); |
103 | | - |
104 | | - // Throw the deserialized exception again. |
105 | | - throw deserExcept; |
106 | | - } |
107 | | - catch( SerializationException se ) |
108 | | - { |
109 | | - Console.WriteLine( "Failed to serialize: {0}", |
110 | | - se.ToString( ) ); |
111 | | - } |
112 | | - finally |
113 | | - { |
114 | | - stream.Close( ); |
115 | | - } |
116 | | - } |
117 | | - } |
118 | | - catch( Exception ex ) |
119 | | - { |
120 | | - Console.WriteLine( "HelpLink: {0}", ex.HelpLink ); |
121 | | - Console.WriteLine( "Source: {0}", ex.Source ); |
122 | | - |
123 | | - Console.WriteLine( ); |
124 | | - Console.WriteLine( ex.ToString( ) ); |
125 | | - } |
126 | | - } |
127 | | - } |
| 6 | + |
| 7 | +// Define a serializable derived exception class. |
| 8 | +[Serializable()] |
| 9 | +class SecondLevelException : Exception, ISerializable |
| 10 | +{ |
| 11 | + // This public constructor is used by class instantiators. |
| 12 | + public SecondLevelException(string message, Exception inner) : |
| 13 | + base(message, inner) |
| 14 | + { |
| 15 | + HelpLink = "http://MSDN.Microsoft.com"; |
| 16 | + Source = "Exception_Class_Samples"; |
| 17 | + } |
| 18 | + |
| 19 | + // This protected constructor is used for deserialization. |
| 20 | + protected SecondLevelException(SerializationInfo info, |
| 21 | + StreamingContext context) : |
| 22 | + base(info, context) |
| 23 | + { } |
| 24 | + |
| 25 | + // GetObjectData performs a custom serialization. |
| 26 | + public override void GetObjectData(SerializationInfo info, |
| 27 | + StreamingContext context) |
| 28 | + { |
| 29 | + // Change the case of two properties, and then use the |
| 30 | + // method of the base class. |
| 31 | + HelpLink = HelpLink.ToLower(); |
| 32 | + Source = Source.ToUpperInvariant(); |
| 33 | + |
| 34 | + base.GetObjectData(info, context); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +class SerializationDemo |
| 39 | +{ |
| 40 | + public static void Main() |
| 41 | + { |
| 42 | + Console.WriteLine( |
| 43 | + "This example of the Exception constructor " + |
| 44 | + "and Exception.GetObjectData\nwith Serialization" + |
| 45 | + "Info and StreamingContext parameters " + |
| 46 | + "generates \nthe following output.\n"); |
| 47 | + |
| 48 | + try |
| 49 | + { |
| 50 | + // This code forces a division by 0 and catches the |
| 51 | + // resulting exception. |
| 52 | + try |
| 53 | + { |
| 54 | + int zero = 0; |
| 55 | + int ecks = 1 / zero; |
| 56 | + } |
| 57 | + catch (Exception ex) |
| 58 | + { |
| 59 | + // Create a new exception to throw again. |
| 60 | + SecondLevelException newExcept = |
| 61 | + new SecondLevelException( |
| 62 | + "Forced a division by 0 and threw " + |
| 63 | + "another exception.", ex); |
| 64 | + |
| 65 | + Console.WriteLine( |
| 66 | + "Forced a division by 0, caught the " + |
| 67 | + "resulting exception, \n" + |
| 68 | + "and created a derived exception:\n"); |
| 69 | + Console.WriteLine("HelpLink: {0}", |
| 70 | + newExcept.HelpLink); |
| 71 | + Console.WriteLine("Source: {0}", |
| 72 | + newExcept.Source); |
| 73 | + |
| 74 | + // This FileStream is used for the serialization. |
| 75 | + FileStream stream = |
| 76 | + new FileStream("NewException.dat", |
| 77 | + FileMode.Create); |
| 78 | + |
| 79 | + try |
| 80 | + { |
| 81 | + // Serialize the derived exception. |
| 82 | + SoapFormatter formatter = |
| 83 | + new SoapFormatter(null, |
| 84 | + new StreamingContext( |
| 85 | + StreamingContextStates.File)); |
| 86 | + formatter.Serialize(stream, newExcept); |
| 87 | + |
| 88 | + // Rewind the stream and deserialize the |
| 89 | + // exception. |
| 90 | + stream.Position = 0; |
| 91 | + SecondLevelException deserExcept = |
| 92 | + (SecondLevelException) |
| 93 | + formatter.Deserialize(stream); |
| 94 | + |
| 95 | + Console.WriteLine( |
| 96 | + "\nSerialized the exception, and then " + |
| 97 | + "deserialized the resulting stream " + |
| 98 | + "into a \nnew exception. " + |
| 99 | + "The deserialization changed the case " + |
| 100 | + "of certain properties:\n"); |
| 101 | + |
| 102 | + // Throw the deserialized exception again. |
| 103 | + throw deserExcept; |
| 104 | + } |
| 105 | + catch (SerializationException se) |
| 106 | + { |
| 107 | + Console.WriteLine("Failed to serialize: {0}", |
| 108 | + se.ToString()); |
| 109 | + } |
| 110 | + finally |
| 111 | + { |
| 112 | + stream.Close(); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + catch (Exception ex) |
| 117 | + { |
| 118 | + Console.WriteLine("HelpLink: {0}", ex.HelpLink); |
| 119 | + Console.WriteLine("Source: {0}", ex.Source); |
| 120 | + |
| 121 | + Console.WriteLine(); |
| 122 | + Console.WriteLine(ex.ToString()); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
128 | 126 | /* |
129 | 127 | This example displays the following output. |
130 | 128 |
|
|
0 commit comments