Book notes in. NET4.0 Object-Oriented Programming-Chapter 1 exception capture and handling

Source: Internet
Author: User

Chapter 4 exception capture and handling

[Summary] exception handling is a problem that must be carefully considered when developing software products. It directly determines the robustness of software products in a large program.

This chapter mainly introduces the. NET exception handling infrastructure and some suggestions for exception handling.

Section 1. NET Exception Handling Basics

1. Exception Overview:

A. Definition: errors caused by running the program. Exceptions are not the same as bugs. They are a manifestation of bugs.

B. Mechanism: It is implemented by CLR and is independent of programming languages. When a program encounters a running error, CLR creates an Exception object. The Exception object is usually an instance of the Exception class (or its derived subclass.

C. If an exception occurs and the application does not write code to handle the exception, the CLR forcibly ends the entire process.

2. try, catch, and finally

Try

{

// Statement that may cause an exception --------- try statement block (1)

}

Catch (exception e)

{

// Statement for exception handling --------- catch statement block (2)

}

Finally

{

// Clean the "Battlefield" Statement --------- finally statement block (3)

}

[Explanation ]:
A. if it runs normally, the execution process is block (1) → block (3 ).

B. If it is not running properly, the execution process is block (1) → block (2) → block (3 ).

C. The finally statement is optional and mainly used to solve resource leakage problems, such as file handle.

3. Exception base class Exception:

A. Important attributes and methods of Exception
A.1 e. GetTpye () obtain the exception type

A.2 e. Message tells the user what happened

A.3 e. StackTrace determines the location where the error occurs. If debugging information is available (the program name. pdb file exists), the source file name and program line number can also be displayed.

A.4 TargetSite: Obtain the method that causes the current exception.

B. Common exceptions

B .1 ArithmeticException: The base class of exceptions that occur during arithmetic operations (such as DivideByZeroException and OverflowException)

B .2 DivideByZeroException

B .3 IndexOutOfRangeException is triggered when you try to use a subscript index array that is less than or beyond the array limit

B .4 The explicit conversion from the base type or interface to the derived type of InvalidCastException fails at runtime, causing this exception.

B .5 NullReferenceException

B .6 OutOfMemoryException is triggered when the memory allocation (through new) fails.

B .7 StackOverflowException this exception is thrown when the execution stack is exhausted because too many pending method calls are saved. It usually indicates that there is a very deep or infinite recursion.

Section 2 handle exceptions during program running

1. capture various types of exceptions

A and C # Use catch statements to only capture objects of the Exception class and its subclass.

B. Catch can only have one parameter. That is, a catch statement can only Catch exceptions of the type specified by this parameter.

C. There are two different catch blocks after a try block. It is a syntax error to catch two exceptions of the same type.

D. The more special the exception type is placed in the front.

2. Exception Handling Mechanism:

A. The default Exception Handling Mechanism of. NET4.0 is to handle only the exceptions of hosted code, but the exceptions of unmanaged code are handled by the operating system.

B. CLR adopts the "two-wheeled traversal" Exception Handling Policy.

B .1 The process of scanning and searching for matched catch clauses is the first round of CLR exception handling process.

B .2 scans and finds the matched finally Clause process, which is the second round of CLR exception handling process.

Section 3 Suggestions on exception handling during actual development

1. Adopt an exception Response Policy: When an exception occurs, capture it and handle it as needed.

2. Follow the basic principles of exception handling:

A. Prevent all unexpected and prevented errors.

B. Handle all unexpected but unavoidable errors.

C. Capture all unexpected errors.

3. Custom exceptions only when necessary.

4. handle exceptions as close as possible.

 

Related Article

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.