C# Type initializer的异常 TypeInitializationException
来源:互联网 发布:js中split 编辑:程序博客网 时间:2024/04/29 17:49
Copy from:http://www.dotnetperls.com/typeinitializationexception
C# TypeInitializationException
TypeInitializationException occurs when a static constructor has an error. It is thrown from static constructors. It actually wraps the errors from static constructors. It cannot be trapped outside of the static constructor reliably.
Example
First, this example program shows that this exception in the .NET Framework is raised when any exception is thrown inside a type initializer, also called a static constructor.
Essentially, the runtime wraps any exception raised in the static constructor inside a TypeInitializationException. The InnerException of the exception is then the original cause of the problem. The TypeInitializationException is only raised from type initializers.
Program that throws TypeInitializationException [C#]using System;class Program{ static Program() {//// Static constructor for the program class.// ... Also called a type initializer.// ... It throws an exception in runtime.//int number = 100;int denominator = int.Parse("0");int result = number / denominator;Console.WriteLine(result); } static void Main() {// Entry point. }}OutputUnhandled Exception: System.TypeInitializationException: The type initializer for'Program' threw an exception. --->System.DivideByZeroException: Attempted to divide by zero. at Program..cctor() in.... --- End of inner exception stack trace --- at Program.Main()
The program contains the Program class, which has a static private constructor and a Main entry point. When the Main entry point is reached, the static Program constructor is executed. The code in the static constructor attempts to divide by zero. This causes a DivideByZeroException to be thrown. The runtime then wraps this exception inside a TypeInitializationException.
Fix
To fix this error, you can add try/catch blocks around the body of the static constructor that throws it. If you try to catch the exception inside the Main method, you will not capture it in this program.
Try KeywordCatch ExamplesHowever:If you wrap the body of the static constructor in a try/catch, you will capture it.
Summary
We demonstrated the TypeInitializationException in the C# language and how it is used to wrap exceptions inside type initializers, also called static constructors. This is a special-purpose exception and often these errors will be fatal to the program's correct execution.
Sometimes:Programs will use static constructors to ensure certain constraints are met at startup. The TypeInitializationException then is thrown when the constraints are not met.
- C# Type initializer的异常 TypeInitializationException
- C#中异常:“The type initializer to throw an exception(类型初始值设定项引发异常)”的简单分析与解决方法
- C# 读取app.config配置文件 节点键值,提示 "System.TypeInitializationException 类型初始值设定项引发异常" 错误的解决方案
- 单元测试System.TypeInitializationException: ......的类型初始值设定项引发异常
- [TypeInitializationException: “th_AdminManagementFunction.ConvertUrl”的类型初始值设定项引发异常。]
- 未处理System.TypeInitializationException异常处理
- System.Web.Http.GlobalConfiguration'的类型初始值引发了异常(The type initializer for 'System.Web.Http.Global
- 由 TypeInitializationException 引起的问题
- 出现错误 未处理的TypeInitializationException
- 使用Spire组件抛出异常The type initializer for 'spr857' threw an exception
- 使用Spire组件抛出异常The type initializer for 'spr857' threw an exception
- [原创]深入理解C# 3.x的新特性(5):Object Initializer 和 Collection Initializer
- “System.TypeInitializationException”类型的异常在 EntityFramework.dll 中发生,但未在用户代码中进行处理
- C#3.0 object initializer
- .net System.TypeInitializationException 类型初始值设定项引发异常
- [原创] 关于Type Initializer和 BeforeFieldInit的问题,看看大家能否给出正确的解释
- 关于Type Initializer和 BeforeFieldInit的问题,看看大家能否给出正确的解释
- variable `xxx' has initializer but incomplete type
- SAP HCM模块中自定义ALV的相关开发
- PHP-ExcelReader:用于解析excel文件的PHP类库
- UNICODE,GBK,UTF-8区别
- Linq查询大全
- Android如何防止apk程序被反编译
- C# Type initializer的异常 TypeInitializationException
- 我的java笔记(高新技术下)
- AppleScript: Creating Complex Dialogs with Ease
- 派号ID
- 为apt-get设置http代理服务器的方法
- Linux系统的特点
- jQuery和Discuz js冲突解决办法
- 3次操作系统重装,传统存储,很有感触~
- 查询或设置文件的属性