Fifth. Exception handling of the management program flow (in. net4.5)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.