Skip to content

Commit bc25f26

Browse files
authored
TypeInitializationException F# snippets (#8017)
1 parent 4f7d2c9 commit bc25f26

File tree

5 files changed

+85
-2
lines changed

5 files changed

+85
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module Missing1
2+
3+
open Missing1a
4+
// <Snippet2>
5+
open System
6+
7+
type Person(fName, lName) =
8+
static let infoModule = InfoModule DateTime.UtcNow
9+
10+
do infoModule.Increment() |> ignore
11+
12+
override _.ToString() =
13+
$"{fName} {lName}"
14+
let p = Person("John", "Doe")
15+
16+
printfn $"{p}"
17+
// The example displays the following output if missing1a.dll is renamed or removed:
18+
// Unhandled Exception: System.TypeInitializationException:
19+
// The type initializer for 'Person' threw an exception. --->
20+
// System.IO.FileNotFoundException: Could not load file or assembly
21+
// 'Missing1a, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
22+
// or one of its dependencies. The system cannot find the file specified.
23+
// at Person..cctor()
24+
// --- End of inner exception stack trace ---
25+
// at Person..ctor(String fName, String lName)
26+
// at Example.Main()
27+
// </Snippet2>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Missing1a
2+
3+
// <Snippet1>
4+
open System
5+
6+
type InfoModule(firstUse: DateTime) =
7+
let mutable ctr = 0
8+
9+
member _.Increment() =
10+
ctr <- ctr + 1
11+
ctr
12+
13+
member _.GetInitializationTime() =
14+
firstUse
15+
// </Snippet1>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// <Snippet4>
2+
open System
3+
open System.Text.RegularExpressions
4+
5+
let domain = AppDomain.CurrentDomain
6+
// Set a timeout interval of -2 seconds.
7+
domain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromSeconds -2)
8+
9+
let rgx = Regex "[aeiouy]"
10+
printfn $"Regular expression pattern: {rgx}"
11+
printfn $"Timeout interval for this regex: {rgx.MatchTimeout.TotalSeconds} seconds"
12+
// The example displays the following output:
13+
// Unhandled Exception: System.TypeInitializationException:
14+
// The type initializer for 'System.Text.RegularExpressions.Regex' threw an exception. --->
15+
// System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
16+
// Parameter name: AppDomain data 'REGEX_DEFAULT_MATCH_TIMEOUT' contains an invalid value or
17+
// object for specifying a default matching timeout for System.Text.RegularExpressions.Regex.
18+
// at System.Text.RegularExpressions.Regex.InitDefaultMatchTimeout()
19+
// at System.Text.RegularExpressions.Regex..cctor()
20+
// --- End of inner exception stack trace ---
21+
// at System.Text.RegularExpressions.Regex..ctor(String pattern)
22+
// at Example.Main()
23+
// </Snippet4>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="Missing1a.fs" />
8+
<Compile Include="Missing1.fs" />
9+
<Compile Include="Regex1.fs" />
10+
</ItemGroup>
11+
</Project>

xml/System/TypeInitializationException.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
<a name="Static"></a>
8181
## Static constructors and the TypeInitializationException
8282
83-
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).
83+
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).
8484
8585
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:
8686
@@ -92,7 +92,7 @@
9292
9393
- It has been explicitly defined as a member of a type.
9494
95-
- 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:
95+
- 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:
9696
9797
```
9898
.method private specialname rtspecialname static
@@ -121,12 +121,17 @@
121121
```csharp
122122
csc -t:library Missing1a.cs
123123
```
124+
125+
```fsharp
126+
fsc --target:library Missing1a.fs
127+
```
124128
125129
```vb
126130
vbc Missing1a.vb -t:library
127131
```
128132
129133
:::code language="csharp" source="~/snippets/csharp/System/TypeInitializationException/Overview/Missing1a.cs" id="Snippet1":::
134+
:::code language="fsharp" source="~/snippets/fsharp/System/TypeInitializationException/Overview/Missing1a.fs" id="Snippet1":::
130135
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/System.TypeInitializationException/vb/Missing1a.vb" id="Snippet1":::
131136
132137
You can then compile the following example to an executable named Missing1.exe by including a reference to Missing1a.dll:
@@ -142,6 +147,7 @@
142147
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.
143148
144149
:::code language="csharp" source="~/snippets/csharp/System/TypeInitializationException/Overview/Missing1.cs" id="Snippet2":::
150+
:::code language="fsharp" source="~/snippets/fsharp/System/TypeInitializationException/Overview/Missing1.fs" id="Snippet2":::
145151
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/System.TypeInitializationException/vb/Missing1.vb" id="Snippet2":::
146152
147153
> [!NOTE]
@@ -154,6 +160,7 @@
154160
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.
155161
156162
:::code language="csharp" source="~/snippets/csharp/System/TypeInitializationException/Overview/Regex1.cs" id="Snippet4":::
163+
:::code language="fsharp" source="~/snippets/fsharp/System/TypeInitializationException/Overview/Regex1.fs" id="Snippet4":::
157164
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/System.TypeInitializationException/vb/Regex1.vb" id="Snippet4":::
158165
159166
<a name="Calendars"></a>

0 commit comments

Comments
 (0)