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

Skip to content

System.ArgumentException F# snippets #7428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module argumentexception
// Types:System.ArgumentException
//<snippet1>
open System

//<snippet2>
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;
//</snippet2>

// 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.
//</snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="argumentexception.fs" />
<Compile Include="argumentexception2.fs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module argumentexception2
// <Snippet3>
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')
//</snippet3>
8 changes: 6 additions & 2 deletions xml/System/ArgumentException.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,16 @@

For a list of initial property values for an instance of <xref:System.ArgumentException>, see the <xref:System.ArgumentException> 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 <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.

:::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":::

]]></format>
Expand Down Expand Up @@ -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":::

]]></format>
</remarks>
</Docs>
Expand Down