Skip to content
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
TypeInitializationException F# snippets
  • Loading branch information
albert-du committed Apr 23, 2022
commit 55285193a7009731e8ea2ad601cb73a1779b1d72
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Missing1

open Missing1a
// <Snippet2>
open System

type Person(fName, lName) =
static let infoModule = InfoModule DateTime.UtcNow

do infoModule.Increment() |> ignore

override _.ToString() =
$"{fName} {lName}"
let p = Person("John", "Doe")

printfn $"{p}"
// The example displays the following output if missing1a.dll is renamed or removed:
// Unhandled Exception: System.TypeInitializationException:
// The type initializer for 'Person' threw an exception. --->
// System.IO.FileNotFoundException: Could not load file or assembly
// 'Missing1a, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
// or one of its dependencies. The system cannot find the file specified.
// at Person..cctor()
// --- End of inner exception stack trace ---
// at Person..ctor(String fName, String lName)
// at Example.Main()
// </Snippet2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Missing1a

// <Snippet1>
open System

type InfoModule(firstUse: DateTime) =
let mutable ctr = 0

member _.Increment() =
ctr <- ctr + 1
ctr

member _.GetInitializationTime() =
firstUse
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// <Snippet4>
open System
open System.Text.RegularExpressions

let domain = AppDomain.CurrentDomain
// Set a timeout interval of -2 seconds.
domain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromSeconds -2)

let rgx = Regex "[aeiouy]"
printfn $"Regular expression pattern: {rgx}"
printfn $"Timeout interval for this regex: {rgx.MatchTimeout.TotalSeconds} seconds"
// The example displays the following output:
// Unhandled Exception: System.TypeInitializationException:
// The type initializer for 'System.Text.RegularExpressions.Regex' threw an exception. --->
// System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
// Parameter name: AppDomain data 'REGEX_DEFAULT_MATCH_TIMEOUT' contains an invalid value or
// object for specifying a default matching timeout for System.Text.RegularExpressions.Regex.
// at System.Text.RegularExpressions.Regex.InitDefaultMatchTimeout()
// at System.Text.RegularExpressions.Regex..cctor()
// --- End of inner exception stack trace ---
// at System.Text.RegularExpressions.Regex..ctor(String pattern)
// at Example.Main()
// </Snippet4>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Missing1a.fs" />
<Compile Include="Missing1.fs" />
<Compile Include="Regex1.fs" />
</ItemGroup>
</Project>
11 changes: 9 additions & 2 deletions xml/System/TypeInitializationException.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<a name="Static"></a>
## Static constructors and the TypeInitializationException

A static constructor, if one exists, is called automatically by the runtime before creating a new instance of a type. Static constructors can be explicitly defined by a developer. If a static constructor is not explicitly defined, compilers automatically create one to initialize any `static` (in C#) or `Shared` (in Visual Basic) members of the type. For more information on static constructors, see [Static Constructors](/dotnet/csharp/programming-guide/classes-and-structs/static-constructors).
A static constructor, if one exists, is called automatically by the runtime before creating a new instance of a type. Static constructors can be explicitly defined by a developer. If a static constructor is not explicitly defined, compilers automatically create one to initialize any `static` (in C# or F#) or `Shared` (in Visual Basic) members of the type. For more information on static constructors, see [Static Constructors](/dotnet/csharp/programming-guide/classes-and-structs/static-constructors).

Most commonly, a <xref:System.TypeInitializationException> exception is thrown when a static constructor is unable to instantiate a type. The <xref:System.Exception.InnerException%2A> property indicates why the static constructor was unable to instantiate the type. Some of the more common causes of a <xref:System.TypeInitializationException> exception are:

Expand All @@ -92,7 +92,7 @@

- It has been explicitly defined as a member of a type.

- The type has `static` (in C#) or `Shared` (in Visual Basic) variables that are declared and initialized in a single statement. In this case, the language compiler generates a static constructor for the type. You can inspect it by using a utility such as [IL Disassembler](/dotnet/framework/tools/ildasm-exe-il-disassembler). For instance, when the C# and VB compilers compile the following example, they generate the IL for a static constructor that is similar to this:
- The type has `static` (in C# or F#) or `Shared` (in Visual Basic) variables that are declared and initialized in a single statement. In this case, the language compiler generates a static constructor for the type. You can inspect it by using a utility such as [IL Disassembler](/dotnet/framework/tools/ildasm-exe-il-disassembler). For instance, when the C# and VB compilers compile the following example, they generate the IL for a static constructor that is similar to this:

```
.method private specialname rtspecialname static
Expand Down Expand Up @@ -121,12 +121,17 @@
```csharp
csc -t:library Missing1a.cs
```

```fsharp
fsc --target:library Missing1a.fs
```

```vb
vbc Missing1a.vb -t:library
```

:::code language="csharp" source="~/snippets/csharp/System/TypeInitializationException/Overview/Missing1a.cs" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/TypeInitializationException/Overview/Missing1a.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/System.TypeInitializationException/vb/Missing1a.vb" id="Snippet1":::

You can then compile the following example to an executable named Missing1.exe by including a reference to Missing1a.dll:
Expand All @@ -142,6 +147,7 @@
However, if you rename, move, or delete Missing1a.dll and run the example, it throws a <xref:System.TypeInitializationException> exception and displays the output shown in the example. Note that the exception message includes information about the <xref:System.Exception.InnerException%2A> property. In this case, the inner exception is a <xref:System.IO.FileNotFoundException> that is thrown because the runtime cannot find the dependent assembly.

:::code language="csharp" source="~/snippets/csharp/System/TypeInitializationException/Overview/Missing1.cs" id="Snippet2":::
:::code language="fsharp" source="~/snippets/fsharp/System/TypeInitializationException/Overview/Missing1.fs" id="Snippet2":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/System.TypeInitializationException/vb/Missing1.vb" id="Snippet2":::

> [!NOTE]
Expand All @@ -154,6 +160,7 @@
The following example shows the <xref:System.TypeInitializationException> that is thrown when the value assigned to the "REGEX_DEFAULT_MATCH_TIMEOUT" property is invalid. To eliminate the exception, set the"REGEX_DEFAULT_MATCH_TIMEOUT" property to a <xref:System.TimeSpan> value that is greater than zero and less than approximately 24 days.

:::code language="csharp" source="~/snippets/csharp/System/TypeInitializationException/Overview/Regex1.cs" id="Snippet4":::
:::code language="fsharp" source="~/snippets/fsharp/System/TypeInitializationException/Overview/Regex1.fs" id="Snippet4":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/System.TypeInitializationException/vb/Regex1.vb" id="Snippet4":::

<a name="Calendars"></a>
Expand Down