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

Skip to content

System.OverflowException F# snippets #7819

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,41 @@
open System

// <Snippet2>
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.
// </Snippet2>

let unchecked () =
// <Snippet3>
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.
// </Snippet3>

// <Snippet1>
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.
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/OverflowException/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="arithmetic1.fs" />
</ItemGroup>
</Project>
5 changes: 4 additions & 1 deletion xml/System/OverflowException.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <xref:System.OverflowException> that is thrown by a multiplication operation that overflows the bounds of the <xref:System.Int32> 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 <xref:System.OverflowException> 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 <xref:System.OverflowException>, 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 <xref:System.OverflowException>, 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 <xref:System.OverflowException>:

Expand Down