Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c202795

Browse files
authored
Add F# snippets for ArgumentException (#7428)
1 parent b2e9db0 commit c202795

File tree

4 files changed

+78
-2
lines changed

4 files changed

+78
-2
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module argumentexception
2+
// Types:System.ArgumentException
3+
//<snippet1>
4+
open System
5+
6+
//<snippet2>
7+
let divideByTwo num =
8+
// If num is an odd number, raise an ArgumentException.
9+
if num % 2 = 1 then
10+
raise (ArgumentException("num", "Number must be even"))
11+
12+
// num is even, return half of its value.
13+
num / 2;
14+
//</snippet2>
15+
16+
// ArgumentException is not thrown because 10 is an even number.
17+
printfn $"10 divided by 2 is {divideByTwo 10}"
18+
try
19+
// ArgumentException is thrown because 7 is not an even number.
20+
printfn $"7 divided by 2 is {divideByTwo 7}"
21+
22+
with
23+
| :? ArgumentException ->
24+
// Show the user that 7 cannot be divided by 2.
25+
printfn "7 is not divided by 2 integrally."
26+
27+
// This code produces the following output.
28+
//
29+
// 10 divided by 2 is 5
30+
// 7 is not divided by 2 integrally.
31+
//</snippet1>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="argumentexception.fs" />
10+
<Compile Include="argumentexception2.fs" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module argumentexception2
2+
// <Snippet3>
3+
open System
4+
5+
let divideByTwo num =
6+
// If num is an odd number, throw an ArgumentException.
7+
if num % 2 = 1 then
8+
invalidArg "num" $"{num} is not an even number"
9+
10+
// num is even, return half of its value.
11+
num / 2;
12+
13+
// Define some integers for a division operation.
14+
let values = [ 10; 7 ]
15+
for value in values do
16+
try
17+
printfn $"{value} divided by 2 is {divideByTwo value}"
18+
with
19+
| :? ArgumentException as e ->
20+
printfn $"{e.GetType().Name}: {e.Message}"
21+
22+
printfn ""
23+
24+
// This example displays the following output:
25+
// 10 divided by 2 is 5
26+
//
27+
// ArgumentException: 7 is not an even number (Parameter 'num')
28+
//</snippet3>

xml/System/ArgumentException.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,16 @@
8383
8484
For a list of initial property values for an instance of <xref:System.ArgumentException>, see the <xref:System.ArgumentException> constructors.
8585
86-
86+
In F#, the [invalidArg](/dotnet/fsharp/language-reference/exception-handling/the-invalidarg-function) function may be used to generate and raise an ArgumentException,
87+
88+
8789
8890
## Examples
8991
The following example demonstrates how to throw and catch an <xref:System.ArgumentException>. It uses the [ArgumentException.GetType().Name](xref:System.Type.Name%2A) property to display the name of the exception object, and also uses the <xref:System.ArgumentException.Message%2A> property to display the text of the exception message.
9092
9193
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/ArgumentException/cpp/argumentexception2.cpp" id="Snippet3":::
9294
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/ArgumentException/CS/argumentexception2.cs" interactive="try-dotnet" id="Snippet3":::
95+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/ArgumentException/FS/argumentexception2.fs" id="Snippet3":::
9396
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/ArgumentException/vb/program2.vb" id="Snippet3":::
9497
9598
]]></format>
@@ -396,7 +399,8 @@
396399
397400
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/ArgumentException/cpp/ArgumentException.cpp" id="Snippet2":::
398401
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/ArgumentException/CS/argumentexception.cs" id="Snippet2":::
399-
402+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/ArgumentException/FS/argumentexception.fs" id="Snippet2":::
403+
400404
]]></format>
401405
</remarks>
402406
</Docs>

0 commit comments

Comments
 (0)