From f991c2de37edb0e12ad8af6956cf4b917b5afc13 Mon Sep 17 00:00:00 2001
From: albert-du <52804499+albert-du@users.noreply.github.com>
Date: Sat, 12 Mar 2022 19:25:04 -0800
Subject: [PATCH] OverflowException F# snippets
---
.../OverflowException/Overview/arithmetic1.fs | 41 +++++++++++++++++++
.../OverflowException/Overview/fs.fsproj | 10 +++++
xml/System/OverflowException.xml | 5 ++-
3 files changed, 55 insertions(+), 1 deletion(-)
create mode 100644 snippets/fsharp/System/OverflowException/Overview/arithmetic1.fs
create mode 100644 snippets/fsharp/System/OverflowException/Overview/fs.fsproj
diff --git a/snippets/fsharp/System/OverflowException/Overview/arithmetic1.fs b/snippets/fsharp/System/OverflowException/Overview/arithmetic1.fs
new file mode 100644
index 00000000000..205276bba1f
--- /dev/null
+++ b/snippets/fsharp/System/OverflowException/Overview/arithmetic1.fs
@@ -0,0 +1,41 @@
+open System
+
+//
+open Checked
+
+let value = 241uy
+try
+ let newValue = int8 value
+ printfn $"Converted the {value.GetType().Name} value {value} to the {newValue.GetType().Name} value {newValue}."
+with :? OverflowException ->
+ printfn $"Exception: {value} > {SByte.MaxValue}."
+// The example displays the following output:
+// Exception: 241 > 127.
+//
+
+let unchecked () =
+ //
+ let value = 241uy
+ try
+ let newValue = int8 value
+ printfn $"Converted the {value.GetType().Name} value {value} to the {newValue.GetType().Name} value {newValue}."
+ with :? OverflowException ->
+ printfn $"Exception: {value} > {SByte.MaxValue}."
+ // The example displays the following output:
+ // Converted the Byte value 241 to the SByte value -15.
+ //
+
+//
+open Checked
+
+let v = 780000000
+try
+ // Square the original value.
+ let square = v * v
+ printfn $"{v} ^ 2 = {square}"
+with :? OverflowException ->
+ let square = float v ** 2
+ printfn $"Exception: {square} > {Int32.MaxValue:E}."
+// The example displays the following output:
+// Exception: 6.084E+17 > 2.147484E+009.
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/OverflowException/Overview/fs.fsproj b/snippets/fsharp/System/OverflowException/Overview/fs.fsproj
new file mode 100644
index 00000000000..547d9bf014f
--- /dev/null
+++ b/snippets/fsharp/System/OverflowException/Overview/fs.fsproj
@@ -0,0 +1,10 @@
+
+
+ Exe
+ net6.0
+
+
+
+
+
+
\ No newline at end of file
diff --git a/xml/System/OverflowException.xml b/xml/System/OverflowException.xml
index 25fc8ce3da1..87340c2ca8d 100644
--- a/xml/System/OverflowException.xml
+++ b/xml/System/OverflowException.xml
@@ -62,18 +62,21 @@
- An arithmetic operation produces a result that is outside the range of the data type returned by the operation. The following example illustrates the that is thrown by a multiplication operation that overflows the bounds of the type.
:::code language="csharp" source="~/snippets/csharp/System/OverflowException/Overview/arithmetic1.cs" interactive="try-dotnet-method" id="Snippet1":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/OverflowException/Overview/arithmetic1.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.overflowexception/vb/arithmetic1.vb" id="Snippet1":::
- A casting or conversion operation attempts to perform a narrowing conversion, and the value of the source data type is outside the range of the target data type. The following example illustrates the that is thrown by the attempt to convert a large unsigned byte value to a signed byte value.
:::code language="csharp" source="~/snippets/csharp/System/OverflowException/Overview/arithmetic1.cs" interactive="try-dotnet-method" id="Snippet2":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/OverflowException/Overview/arithmetic1.fs" id="Snippet2":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.overflowexception/vb/arithmetic1.vb" id="Snippet2":::
In each case, the result of the operation is a value that is less than the `MinValue` property or greater than the `MaxValue` property of the data type that results from the operation.
- For the arithmetic, casting, or conversion operation to throw an , the operation must occur in a checked context. By default, arithmetic operations and overflows in Visual Basic are checked; in C#, they are not. If the operation occurs in an unchecked context, the result is truncated by discarding any high-order bits that do not fit into the destination type. The following example illustrates such an unchecked conversion in C#. It repeats the previous example in an unchecked context.
+ For the arithmetic, casting, or conversion operation to throw an , the operation must occur in a checked context. By default, arithmetic operations and overflows in Visual Basic are checked; in C# and F#, they are not. If the operation occurs in an unchecked context, the result is truncated by discarding any high-order bits that do not fit into the destination type. The following example illustrates such an unchecked conversion in C# or F#. It repeats the previous example in an unchecked context.
:::code language="csharp" source="~/snippets/csharp/System/OverflowException/Overview/arithmetic1.cs" interactive="try-dotnet-method" id="Snippet3":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/OverflowException/Overview/arithmetic1.fs" id="Snippet3":::
The following Microsoft intermediate language (MSIL) instructions throw an :