Exception handling mechanism

Source: Internet
Author: User
Tags finally block stack trace

Directory

    • The structure of the exception and the Order of execution
    • System.Exception Exception class

I. The structure of the exception and the Order of execution

The first paragraph of code: A is how much?

 intA =0; Try{a++; Throw NewException (); }            Catch(Exception ex) {Try{a++; return; }                finally{a++; }            }            finally{a++; }

Let's start with the three-block running relationship (try, catch, finally).

Try block: Whenever possible, place code that is more prone to exceptions in a try block, and a try block matches at least one catch block or finally block. The try{}catch{} block may still be contained in a try block.

Catch block: 1 is a response to an exception that is fetched, and the try block may have 0 or more catch blocks associated with it. Does not fetch an exception, skips the catch block, executes the finally block (if any). You can grab the exception and execute the code in the Catch block.

2. The type to be captured must be a System.Exception type or a derived type. Throws an exception, which is matched from top to bottom, so the exception type with the smaller range is placed above.

If there is no match to the exception type, it throws a type for processing. If the exception type is matched, the finally block of the inner layer is executed, and after the code in the Catch block executes, the finally block that matches the catch block is executed.

      

Finally block: Operations such as releasing resources are definitely performed. Avoid throwing exceptions again in finally.

Second, System.Exception class

 //Summary://gets a collection of key/value pairs that provide additional user-defined exception information. //        //return Result://an object that implements the System.Collections.IDictionary interface and contains a collection of user-defined key/value pairs. The default value is an empty collection.          Public VirtualIDictionary Data {Get; } //        //Summary://Gets or sets the link to the Help file associated with this exception. //        //return Result://A Uniform Resource name (URN) or Uniform Resource Locator (URL).          Public Virtual stringHelpLink {Get;Set; } //        //Summary://Gets or sets the HRESULT (an encoded numeric value assigned to a specific exception). //        //return Result://The HRESULT value.          Public intHResult {Get;protected Set; } //        //Summary://Gets the System.Exception instance that caused the current exception. //        //return Result://an instance of Exception that describes the error that caused the current exception. The InnerException property returns the same value as the value passed to the constructor, or, if no intrinsic exception value is supplied to the constructor, returns//a null reference (Nothing in Visual Basic). This property is read-only.          PublicException innerexception {Get; } //        //Summary://Gets the message that describes the current exception. //        //return Result://an error message or an empty string ("") that explains the cause of the exception.          Public Virtual stringMessage {Get; } //        //Summary://Gets or sets the name of the application or object that caused the error. //        //return Result://The name of the application or object that caused the error. //        //Exception://System.ArgumentException://The object must be a run-time System.Reflection object         Public Virtual stringSource {Get;Set; } //        //Summary://gets the string representation of the direct frame on the call stack. //        //return Result://A string that describes the direct frame of the call stack.          Public Virtual stringStackTrace {Get; } //        //Summary://gets the method that raised the current exception. //        //return Result://the System.Reflection.MethodBase that raised the current exception.          PublicMethodBase TargetSite {Get; }
View Code

The Stacktrace:catch block can read this property to get a stack trace and can get the method called before the exception is thrown. If we construct a new exception class, the StackTrace property is reset to null. Accessing this property actually executes the CLR code.

If an exception is thrown, the CLR records where the exception is thrown, the catch block snaps to the exception, and the CLR records the location where the exception was caught. We access the StackTrace property of the exception object within the catch block, actually invoking the CLR code, generating a string containing all the methods that are called from the exception location to the snap location.

Subsequent...

Exception handling mechanism

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.