C # Exception Handling Method

Source: Internet
Author: User

 

C #Exception Handling Method

 

C #All exceptions that can be thrown in are directly or indirectly inherited fromSystem. ExceptionClass

 

The following statement blocks are supported to capture exceptions:

Try... Catch

Try... Catch... Finally

Try... Finally

 

C #CodeWhen the exception stack information is generated in the block is notThrowWhere the statement is executed, but where it is captured for the first time

 

In the preceding three methodsTry... finallyStack information will not be affected

The areas that may be affected are mainly concentrated inCatchBlock

 

CatchClause statements can be declared in the following ways:

 

Catch {}

Catch (exception ){}

Catch (exception ex ){}

 

FromAbility to capture exceptionsBasically, it is equivalent.

The third method is to allow the person who writes the code to use the exception parameter. If the exception parameter is not used, the first two methods can be used. The second method only explicitly indicates thatExceptionOr fromExceptionInheritance exception

 

CatchThe block can throw an exception again. The following methods are supported during throw:

 

Throw;

Throw ex; // exFromCatch (exception ex)

Throw new someexception ();

 

The two situations are the same,Stack traceThink youCatchThe exception has been processed, but a new exception is thrown during the processing.Stack traceJust putThrowThen the abnormal instance is treated as the root cause of the error. All previous stack information will be cleared

Method 1 online andMsdnStack information can be retained. The test showed that this was not all true:

InDebugThe generated code is directly used without any optimization during execution.Throw;This method can indeed retain stack information. The stack information at this time is likeTryThe cause of internal block exceptions is recorded as well.

InReleaseThe output information only contains the stack information starting from the capture position. That is, from the origin location of the exception to the first timeCatchThe stack information between block capturing does not exist anymore.

 

Summary:

The previous exception information contains the error points that can be easily found, because it is a coincidence that the source is very close to the place captured for the first time.

Otherwise, the more exceptions are caught at the top of the call, the less difficult it is to find the location of the exception source (hereRelease)

InReleaseIn a version, the earlier an exception is captured, the more accurate the error location is. The later the exception is captured, the less valid information is obtained from the stack.Throw;The same way

Strictly prohibited The following statement:

Try {

// Do

}

Catch (exception ex)

{

Throw ex;

}

SwitchThrow;OrThrow new someexception (MSG, ex );This form

 

If an exception is caught and you do not want to throw it again (the exception is usually used within the expected range and the corresponding processing method already exists), it is best to record the log, indicating that an exception has occurred.

 

 

Attached a test:

ReleaseLower UseThrow;

ReleaseLower UseThrow ex;

 

 

ReleaseThe more extreme example removes all except the outermost layerTry... Catch:

 

 

 

DebugLower UseThrow;

 

 

DebugLower UseThrow ex;

 

 

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.