From 2edb89392b2dca6fa1b4d44a749a11cc22cf56aa Mon Sep 17 00:00:00 2001
From: albert-du <52804499+albert-du@users.noreply.github.com>
Date: Fri, 26 Nov 2021 16:02:41 -0800
Subject: [PATCH] Add F# snippets for ArgumentException
---
.../ArgumentException/FS/argumentexception.fs | 31 +++++++++++++++++++
.../FS/argumentexception.fsproj | 13 ++++++++
.../FS/argumentexception2.fs | 28 +++++++++++++++++
xml/System/ArgumentException.xml | 8 +++--
4 files changed, 78 insertions(+), 2 deletions(-)
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR/ArgumentException/FS/argumentexception.fs
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR/ArgumentException/FS/argumentexception.fsproj
create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR/ArgumentException/FS/argumentexception2.fs
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR/ArgumentException/FS/argumentexception.fs b/samples/snippets/fsharp/VS_Snippets_CLR/ArgumentException/FS/argumentexception.fs
new file mode 100644
index 00000000000..d7be681b35c
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR/ArgumentException/FS/argumentexception.fs
@@ -0,0 +1,31 @@
+module argumentexception
+// Types:System.ArgumentException
+//
+open System
+
+//
+let divideByTwo num =
+ // If num is an odd number, raise an ArgumentException.
+ if num % 2 = 1 then
+ raise (ArgumentException("num", "Number must be even"))
+
+ // num is even, return half of its value.
+ num / 2;
+//
+
+// ArgumentException is not thrown because 10 is an even number.
+printfn $"10 divided by 2 is {divideByTwo 10}"
+try
+ // ArgumentException is thrown because 7 is not an even number.
+ printfn $"7 divided by 2 is {divideByTwo 7}"
+
+with
+| :? ArgumentException ->
+ // Show the user that 7 cannot be divided by 2.
+ printfn "7 is not divided by 2 integrally."
+
+// This code produces the following output.
+//
+// 10 divided by 2 is 5
+// 7 is not divided by 2 integrally.
+//
\ No newline at end of file
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR/ArgumentException/FS/argumentexception.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR/ArgumentException/FS/argumentexception.fsproj
new file mode 100644
index 00000000000..e8aceb818ae
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR/ArgumentException/FS/argumentexception.fsproj
@@ -0,0 +1,13 @@
+
+
+
+ Exe
+ net6.0
+
+
+
+
+
+
+
+
diff --git a/samples/snippets/fsharp/VS_Snippets_CLR/ArgumentException/FS/argumentexception2.fs b/samples/snippets/fsharp/VS_Snippets_CLR/ArgumentException/FS/argumentexception2.fs
new file mode 100644
index 00000000000..681e26485c2
--- /dev/null
+++ b/samples/snippets/fsharp/VS_Snippets_CLR/ArgumentException/FS/argumentexception2.fs
@@ -0,0 +1,28 @@
+module argumentexception2
+//
+open System
+
+let divideByTwo num =
+ // If num is an odd number, throw an ArgumentException.
+ if num % 2 = 1 then
+ invalidArg "num" $"{num} is not an even number"
+
+ // num is even, return half of its value.
+ num / 2;
+
+// Define some integers for a division operation.
+let values = [ 10; 7 ]
+for value in values do
+ try
+ printfn $"{value} divided by 2 is {divideByTwo value}"
+ with
+ | :? ArgumentException as e ->
+ printfn $"{e.GetType().Name}: {e.Message}"
+
+ printfn ""
+
+// This example displays the following output:
+// 10 divided by 2 is 5
+//
+// ArgumentException: 7 is not an even number (Parameter 'num')
+//
diff --git a/xml/System/ArgumentException.xml b/xml/System/ArgumentException.xml
index f7155586a50..0be3b496fb3 100644
--- a/xml/System/ArgumentException.xml
+++ b/xml/System/ArgumentException.xml
@@ -83,13 +83,16 @@
For a list of initial property values for an instance of , see the constructors.
-
+ In F#, the [invalidArg](/dotnet/fsharp/language-reference/exception-handling/the-invalidarg-function) function may be used to generate and raise an ArgumentException,
+
+
## Examples
The following example demonstrates how to throw and catch an . It uses the [ArgumentException.GetType().Name](xref:System.Type.Name%2A) property to display the name of the exception object, and also uses the property to display the text of the exception message.
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/ArgumentException/cpp/argumentexception2.cpp" id="Snippet3":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/ArgumentException/CS/argumentexception2.cs" interactive="try-dotnet" id="Snippet3":::
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/ArgumentException/FS/argumentexception2.fs" id="Snippet3":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/ArgumentException/vb/program2.vb" id="Snippet3":::
]]>
@@ -396,7 +399,8 @@
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/ArgumentException/cpp/ArgumentException.cpp" id="Snippet2":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/ArgumentException/CS/argumentexception.cs" id="Snippet2":::
-
+ :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/ArgumentException/FS/argumentexception.fs" id="Snippet2":::
+
]]>