1. Overview
This chapter includes parts related to exception handling in. net4.5.
2. Main content
2.1 Handling Exceptions
①try, Cahtch, finally mechanism, need not say much.
② using the Environment.failfast method, you can immediately terminate the program and write to the system event log. Will bypass the finally execution.
Public Static voidMain () {strings =Console.ReadLine (); Try { inti =int. Parse (s); if(i = = the) environment.failfast ( "Special number entered. "); } finally{Console.WriteLine ("Program Complete ."); } }
③ if the code in finally has an exception, it will be thrown into the outer handler and the initial exception information will be lost.
2.2 Throwing Exceptions
① do not attempt to reuse exception objects, especially in multithreaded environments. Because the stack information for the exception may be changed by other threads.
②c#5 adds a new Exceptiondispatchinfo.throwthat will save the original exception information and throw it up. It is mainly used for passing exception information between threads.
NULL ; Try { string s = console.readline (); int . Parse (s);} Catch (FormatException ex) { = exceptiondispatchinfo.capture (ex);} if NULL ) Possibleexception.throw ();
③ exception handling can affect program flow and performance and reduce readability.
④ some exceptions that can only be thrown by the runtime: Airthmaticexception, ArrayTypeMismatchException, DivideByZeroException,
IndexOutOfRangeException, InvalidCastException, NullReferenceException, OutOfMemoryException,
OverflowException, StackOverflowException, TypeInitializationException.
2.3 Creating a Custom exception
Custom exceptions should follow the rules, ending with exception. The serialization attribute should be added. Do not inherit from System.ApplicationException.
[Serializable] Public classorderprocessingexception:exception, iserializable{ PublicOrderprocessingexception (intorderId) {OrderId=orderId; This. HelpLink ="Http://www.mydomain.com/infoExps"; } PublicOrderprocessingexception (intOrderId,stringmsg):Base(msg) {OrderId=orderId; This. HelpLink ="Http://www.mydomain.com/infoExps"; } PublicOrderprocessingexception (intOrderId,stringmsg, Exception innerexp):Base(msg, innerexp) {OrderId=orderId; This. HelpLink ="Http://www.mydomain.com/infoExps"; } protectedorderprocessingexception (SerializationInfo info, StreamingContext context) {OrderId= (int) info. GetValue ("OrderId",typeof(int)); } Public intOrderId {Get;Private Set;} Public voidGetObjectData (SerializationInfo info, StreamingContext context) {info. AddValue ("OrderId", OrderId,typeof(int)); }}
3. Summary
① in. NET, exceptions should be handled with the exception handling mechanism provided.
② should use a try block with one or more catch blocks to handle different types of exceptions.
③ a custom exception should be used only when it is determined that the user of the code will be able to handle it logically. Otherwise, the built-in exception type should be used.
Fifth. Exception handling of the management program flow (in. net4.5)