|
| 1 | +module NDP_UE_FS_3 |
| 2 | + |
| 3 | +//<Snippet1> |
| 4 | +open System |
| 5 | +open System.IO |
| 6 | +open System.Runtime.Serialization |
| 7 | +open System.Runtime.Serialization.Formatters.Soap |
| 8 | +open System.Security.Permissions |
| 9 | + |
| 10 | + // Define a serializable derived exception class. |
| 11 | + [<Serializable>] |
| 12 | + type SecondLevelException = |
| 13 | + inherit Exception |
| 14 | + |
| 15 | + interface ISerializable with |
| 16 | + // GetObjectData performs a custom serialization. |
| 17 | + [<SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter=true)>] |
| 18 | + member this.GetObjectData(info: SerializationInfo, context: StreamingContext) = |
| 19 | + // Change the case of two properties, and then use the |
| 20 | + // method of the base class. |
| 21 | + this.HelpLink <- this.HelpLink.ToLower() |
| 22 | + this.Source <- this.Source.ToUpperInvariant() |
| 23 | + |
| 24 | + base.GetObjectData( info, context ) |
| 25 | + // This public constructor is used by class instantiators. |
| 26 | + new (message: string, inner: Exception) as this = |
| 27 | + { inherit Exception(message, inner) } |
| 28 | + then |
| 29 | + this.HelpLink <- "http://MSDN.Microsoft.com" |
| 30 | + this.Source <- "Exception_Class_Samples" |
| 31 | + |
| 32 | + // This protected constructor is used for deserialization. |
| 33 | + new (info: SerializationInfo, context: StreamingContext) = |
| 34 | + { inherit Exception(info, context) } |
| 35 | + |
| 36 | +printfn |
| 37 | + """This example of the Exception constructor and Exception.GetObjectData |
| 38 | +with SerializationInfo and StreamingContext parameters generates |
| 39 | +the following output. |
| 40 | +""" |
| 41 | + |
| 42 | +try |
| 43 | + // This code forces a division by 0 and catches the |
| 44 | + // resulting exception. |
| 45 | + try |
| 46 | + let zero = 0 |
| 47 | + let ecks = 1 / zero |
| 48 | + () |
| 49 | + with ex -> |
| 50 | + // Create a new exception to throw again. |
| 51 | + let newExcept = SecondLevelException("Forced a division by 0 and threw another exception.", ex) |
| 52 | + |
| 53 | + printfn "Forced a division by 0, caught the resulting exception, \nand created a derived exception:\n" |
| 54 | + printfn $"HelpLink: {newExcept.HelpLink}" |
| 55 | + printfn $"Source: {newExcept.Source}" |
| 56 | + |
| 57 | + // This FileStream is used for the serialization. |
| 58 | + use stream = new FileStream("NewException.dat", FileMode.Create) |
| 59 | + |
| 60 | + try |
| 61 | + // Serialize the derived exception. |
| 62 | + let formatter = SoapFormatter(null, StreamingContext StreamingContextStates.File) |
| 63 | + formatter.Serialize(stream, newExcept) |
| 64 | + |
| 65 | + // Rewind the stream and deserialize the |
| 66 | + // exception. |
| 67 | + stream.Position <- 0L |
| 68 | + let deserExcept = formatter.Deserialize stream :?> SecondLevelException |
| 69 | + |
| 70 | + printfn |
| 71 | + """ |
| 72 | +Serialized the exception, and then deserialized the resulting stream into a |
| 73 | +new exception. The deserialization changed the case of certain properties: |
| 74 | +""" |
| 75 | + |
| 76 | + // Throw the deserialized exception again. |
| 77 | + raise deserExcept |
| 78 | + with :? SerializationException as se -> |
| 79 | + printfn $"Failed to serialize: {se}" |
| 80 | + |
| 81 | +with ex -> |
| 82 | + printfn $"HelpLink: {ex.HelpLink}" |
| 83 | + printfn $"Source: {ex.Source}" |
| 84 | + printfn $"\n{ex}" |
| 85 | + |
| 86 | +// This example displays the following output. |
| 87 | +// Forced a division by 0, caught the resulting exception, |
| 88 | +// and created a derived exception: |
| 89 | +// |
| 90 | +// HelpLink: http://MSDN.Microsoft.com |
| 91 | +// Source: Exception_Class_Samples |
| 92 | +// |
| 93 | +// Serialized the exception, and then deserialized the resulting stream into a |
| 94 | +// new exception. The deserialization changed the case of certain properties: |
| 95 | +// |
| 96 | +// HelpLink: http://msdn.microsoft.com |
| 97 | +// Source: EXCEPTION_CLASS_SAMPLES |
| 98 | +// |
| 99 | +// NDP_UE_FS_3+SecondLevelException: Forced a division by 0 and threw another except |
| 100 | +// ion. ---> System.DivideByZeroException: Attempted to divide by zero. |
| 101 | +// at <StartupCode$fs>.$NDP_UE_FS_3.main@() |
| 102 | +// --- End of inner exception stack trace --- |
| 103 | +// at <StartupCode$fs>.$NDP_UE_FS_3.main@() |
| 104 | +//</Snippet1> |
0 commit comments