C # New Methods of unified error capturing and handling

Source: Internet
Author: User

Http://lwbpeter.blog.163.com/blog/static/38508211201007105437493/

A three-tier architecture + WCF + nhib.pdf c/s project was developed in the recent period. It has always been a headache to handle error capture in the project,

Today I finally found an acceptable solution.

First, capture all the errors of unprocessed threads in the foreground for unified processing.

For details, see solution for unhandled exceptions in winform.

Second, it is the error handling in the WCF Service background. Before the error contract returns the error to the foreground, it records the error information to the error log.

See:

Step-by-Step WCF distributed development: Win (15): faultcontract and exceptionhandle)

Finally, a simple and flexible error handling method is required. You do not need to write a try-Catch Block, but can also process errors in a unified manner.

As expected, the function is to use the try-Catch Block to wrap the method to be executed. I searched for it on the internet all over the day, and I went to check out C # AOP,

Not ideal,

After thinking for a long time, suddenly there was a glimmer of aura, so there was the following code:

The principle is the C # delegation mechanism.

/// <Summary>
/// Method to be executed
/// </Summary>
Public Delegate void methodtodo ();
/// <Summary>
/// Method for handling errors
/// </Summary>
/// <Param name = "E"> error object </param>
Public Delegate void methoddealexception (exception E );

/// <Summary>
/// Enclose the method with a try-Catch Block
/// </Summary>
Public class trycatch
{
Private event methodtodo;
Private event methoddealexception;

/// <Summary>
/// Enclose the method with a try-Catch Block
/// </Summary>
/// <Param name = "methodtodo"> method to be executed </param>
Public trycatch (methodtodo)
{
This. methodtodo + = methodtodo;
Do ();
}

/// <Summary>
/// Enclose the method with a try-Catch Block
/// </Summary>
/// <Param name = "methodtodo"> method to be executed </param>
/// <Param name = "methoddealexception"> method of error handling </param>
Public trycatch (methodtodo, methoddealexception)
{
This. methodtodo + = methodtodo;
This. methoddealexception + = methoddealexception;
Do ();
}

Public Void Do ()
{
If (this. methodtodo! = NULL)
{
// Obtain the delegate list
Delegate [] arr = This. methodtodo. getinvocationlist ();

Foreach (delegate D in ARR)
{
Try
{
Methodtodo method = (methodtodo) D;
// Synchronous execution Method
Method. Invoke ();
}
Catch (exception ex)
{
If (this. methoddealexception! = NULL) // execute the custom error handling method
{
Delegate [] err = This. methodtodo. getinvocationlist ();
Foreach (Delegate De in ERR)
{
Methoddealexception me = (methoddealexception) de;
Me. Invoke (Ex );
}
}
Else // executes a unified Error Handling Method
{
Throw ex;
}
}
}
}
}
}

The preceding error handling class can be called as follows:

First declare the method to be executed, such:

Private void CAL ()
{
Int I = calint ("");
}

Private int calint (string P)
{
Return Int. parse (P );
}

Then you can call the processing class for execution:

New trycatch (CAL );

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.