C# System.TypeInitializationException异常如何处理
导读:本文共5291.5字符,通常情况下阅读需要18分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: C# System.TypeInitializationException 异常处理备忘问题在这这种错误大多是声明的类里面初始字段赋值出了问题比如 在类里面生命了一个 太大的数组,超出了最大内存限制就会出错修改下就OK了C#基础--错误和异常异常类在c#中,当出现某个特殊的异常错误条件时,就会创建(或抛出)一个异常对象。这个对象包含有助于跟踪问 题的信息。我们可... ...
目录
(为您整理了一些要点),点击可以直达。C# System.TypeInitializationException 异常处理
备忘
问题在这
这种错误大多是声明的类里面初始字段赋值出了问题
比如 在类里面生命了一个 太大的数组,超出了最大内存限制就会出错
修改下就OK了
C#基础--错误和异常
异常类
在c#中,当出现某个特殊的异常错误条件时,就会创建(或抛出)一个异常对象。这个对象包含有助于跟踪问 题的信息。我们可以创建自己的异常类,但.NET提供了许多预定义的异常类,多到这里不可能 提供详尽的列表。
列举几个常见异常:
StackOverflowException
—如果分配给栈的内存区域己满,就会抛出这个异常。EndOfStreamException
—这个异常通常是因为读到文件末尾而抛出的。OverflowException
—如果要在checked上下文中把包含值-40的int类型数据强制转换为uint数据,就会抛出这个异常。
捕获异常
try
块包含的代码组成了程序的正常操作部分,但这部分程序可能遇到某些严重的错误。catch
块包含的代码处理各种错误情况,这些错误是执行try块中的代码时遇到的。这个块还可以用于记 录错误。finally
块包含的代码清理资源或执行通常要在try块或catch块末尾执行的其他操作。无论是否抛出异常,都会执行finally块,理解这一点非常重要。因为finally块包含了应总是执行的清理代码,如果 在finally块中放置了return语句,编译器就会标记一个错误。
下面的步骤说明了这些块是如何组合在一起捕获错误情况的:
(1) 执行的程序流进入try块。
(2) 如果在try块中没有错误发生,在块中就会正常执行操作。当程序流到达try块末尾后,如果存在一个finally块,程序流就会自动SA finally块(第(5)步)。但如果在try块中程序流检测到一个错误,程序流就会跳转 到catch块(第⑶步)。
(3) 在catch块中处理错误。
(4) 在catch块执行完后,如果存在一个finally块,程序流就会自动进入finally块:
(5) 执行finally块(如果存在)。
try{}catch(Exceptionex){}finally{}
异常性能
异常处理具有性能含义。在常见的情况下,不应该使用异常处理错误。例如,将字符串转换为数字时,可 以使用int类型的Paree方法。如果传递给此方法的字符串不能转换为数字,此方法抛FormatException异常;如果可以转换一个数字,但它不能放在int类型中,则抛出OverflowException异常:
staticvoidNumberDemol(stringn){if(nisnull)thrownewArgumentNullException(nameof(n));try{inti=int.Parse(n);Console.WriteLine($"converted:{i}");}catch(FormatExceptionex){Console.WriteLine(ex.Message);}catch(OverflowExceptionex){Console.WriteLine(ex.Message);}}
如果NumberDemol方法通常只用于在字符串中传递数字而接收不到数字是异常的,那么可以这样编写它。 但是,如果在程序流的正常情况下,期望的字符串不能转换时,可以使用TryParse方法。如果字符串不能转换 为数字,此方法不会抛出异常。相反,如果解析成功,TryParse返回true;如果解析失败,则返回felse:
staticvoidNumberDemo2(stringn){if(nisnull)thrownewArgumentNullException(nameof(n));if(int.TryParse(n,outintresult)){Console.WriteLine($"converted{result}");}else{Console.WriteLine("notanumber");}}
实现多个catch块
classProgram{staticvoidMain(){while(true){try{stringuserInput;Console.Write("Inputanumberbetween0and5orjusthitreturntoexit)>");userInput=Console.ReadLine();if(string.IsNullOrEmpty(userInput)){break;}intindex=Convert.ToInt32(userInput);if(index<0||index>5){thrownewIndexOutOfRangeException($"Youtypedin{userInput}");}Console.WriteLine($"Yournumberwas{index}");}catch(IndexOutOfRangeExceptionex){Console.WriteLine($"Exception:Numbershouldbebetween0and5.{ex.Message}");}catch(Exceptionex){Console.WriteLine($"Anexceptionwasthrown.Messagewas:{ex.Message}");}finally{Console.WriteLine("Thankyou\n");}}}}
异常过滤器
自从C# 6开始就支持异常过滤器。catck块仅在过滤器返回true时执行。捕获不同的异常类型时,可以有行为不同的catch块。在某些情况下,catch块基于异常的内容执行不同的操作。
classProgram{staticvoidMain(){try{ThrowWithErrorCode(405);}catch(MyCustomExceptionex)when(ex.ErrorCode==405){Console.WriteLine($"Exceptioncaughtwithfilter{ex.Message}and{ex.ErrorCode}");}catch(MyCustomExceptionex){Console.WriteLine($"Exceptioncaught{ex.Message}and{ex.ErrorCode}");}Console.ReadLine();}publicstaticvoidThrowWithErrorCode(intcode){thrownewMyCustomException("ErrorinFoo"){ErrorCode=code};}}
自定义异常
这个示例称为SolicitColdCall,它包 含两个嵌套的try块,说明了如何定义自定义异常类,再从try块中抛出另一个异常。
publicclassColdCallFileFormatException:Exception{publicColdCallFileFormatException(stringmessage):base(message){}publicColdCallFileFormatException(stringmessage,ExceptioninnerException):base(message,innerException){}}publicclassSalesSpyFoundException:Exception{publicSalesSpyFoundException(stringspyName):base($"Salesspyfound,withname{spyName}"){}publicSalesSpyFoundException(stringspyName,ExceptioninnerException):base($"Salesspyfoundwithname{spyName}",innerException){}}publicclassUnexpectedException:Exception{publicUnexpectedException(stringmessage):base(message){}publicUnexpectedException(stringmessage,ExceptioninnerException):base(message,innerException){}}publicclassColdCallFileReader:IDisposable{privateFileStream_fs;privateStreamReader_sr;privateuint_nPeopleToRing;privatebool_isDisposed=false;privatebool_isOpen=false;publicvoidOpen(stringfileName){if(_isDisposed){thrownewObjectDisposedException("peopleToRing");}_fs=newFileStream(fileName,FileMode.Open);_sr=newStreamReader(_fs);try{stringfirstLine=_sr.ReadLine();_nPeopleToRing=uint.Parse(firstLine);_isOpen=true;}catch(FormatExceptionex){thrownewColdCallFileFormatException($"Firstlineisn\'taninteger{ex}");}}publicvoidProcessNextPerson(){if(_isDisposed){thrownewObjectDisposedException("peopleToRing");}if(!_isOpen){thrownewUnexpectedException("Attemptedtoaccesscoldcallfilethatisnotopen");}try{stringname=_sr.ReadLine();if(name==null){thrownewColdCallFileFormatException("Notenoughnames");}if(name[0]=='B'){thrownewSalesSpyFoundException(name);}Console.WriteLine(name);}catch(SalesSpyFoundExceptionex){Console.WriteLine(ex.Message);}finally{}}publicuintNPeopleToRing{get{if(_isDisposed){thrownewObjectDisposedException("peopleToRing");}if(!_isOpen){thrownewUnexpectedException("Attemptedtoaccesscold–callfilethatisnotopen");}return_nPeopleToRing;}}publicvoidDispose(){if(_isDisposed){return;}_isDisposed=true;_isOpen=false;_fs?.Dispose();_fs=null;}}classProgram{staticvoidMain(){Console.Write("Pleasetypeinthenameofthefile"+"containingthenamesofthepeopletobecoldcalled>");stringfileName=Console.ReadLine();ColdCallFileReaderLoop1(fileName);Console.WriteLine();ColdCallFileReaderLoop2(fileName);Console.WriteLine();Console.ReadLine();}privatestaticvoidColdCallFileReaderLoop2(stringfileName){using(varpeopleToRing=newColdCallFileReader()){try{peopleToRing.Open(fileName);for(inti=0;i<peopleToRing.NPeopleToRing;i++){peopleToRing.ProcessNextPerson();}Console.WriteLine("Allcallersprocessedcorrectly");}catch(FileNotFoundException){Console.WriteLine($"Thefile{fileName}doesnotexist");}catch(ColdCallFileFormatExceptionex){Console.WriteLine($"Thefile{fileName}appearstohavebeencorrupted");Console.WriteLine($"Detailsofproblemare:{ex.Message}");if(ex.InnerException!=null){Console.WriteLine($"Innerexceptionwas:{ex.InnerException.Message}");}}catch(Exceptionex){Console.WriteLine($"Exceptionoccurred:\n{ex.Message}");}}}publicstaticvoidColdCallFileReaderLoop1(stringfileName){varpeopleToRing=newColdCallFileReader();try{peopleToRing.Open(fileName);for(inti=0;i<peopleToRing.NPeopleToRing;i++){peopleToRing.ProcessNextPerson();}Console.WriteLine("Allcallersprocessedcorrectly");}catch(FileNotFoundException){Console.WriteLine($"Thefile{fileName}doesnotexist");}catch(ColdCallFileFormatExceptionex){Console.WriteLine($"Thefile{fileName}appearstohavebeencorrupted");Console.WriteLine($"Detailsofproblemare:{ex.Message}");if(ex.InnerException!=null){Console.WriteLine($"Innerexceptionwas:{ex.InnerException.Message}");}}catch(Exceptionex){Console.WriteLine($"Exceptionoccurred:\n{ex.Message}");}finally{peopleToRing.Dispose();}}}
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
C# System.TypeInitializationException异常如何处理的详细内容,希望对您有所帮助,信息来源于网络。