EnterLib5.0 Hands-on Exception Handling

Source: Internet
Author: User

I have been reading the enterprise database for some time. From this article, I plan to use the example on EnterLib5.0 Hands-on, this document introduces the basic usage of enterprise databases in a series based on my practices and understandings. EnterLib5.0 Hands-on is a DEMO made by some cool people. If you are interested, download it and check it out. Some of them are translated by me and may not be accurate. I am using version 5.0. The VS version is 2008 SP1.

Let's start with the first exception handling article. I hope the experts can point out some deviations.

Example: Check the entered characters. If the entered characters contain numbers, the program throws an exception.

In this example, you need to add the following application assembly:

1. Microsoft. Practices. EnterpriseLibrary. Common. dll

2. Microsoft. Practices. EnterpriseLibrary. ExceptionHandling. dll

3. Microsoft. Practices. EnterpriseLibrary. ExceptionHandling. Logging. dll

4. Microsoft. Practices. EnterpriseLibrary. ServiceLocation. dll

Although only the ExceptionHandling assembly is an exception handling API, other Assembly also needs to be in the Bin \ Debug directory. They provide other functions for program exception handling.

The code in the event of checking the button for adding words is as follows:

 1          try
2 {
3 // TODO: Handle exceptions
4   PuzzlerService.Dictionary.AddWord(txtWordToCheck.Text);
5 errorProvider1.SetError(txtWordToCheck, "");
6 }
7 catch (Exception ex)
8 {
9 bool exceptionResult = ExceptionPolicy.HandleException(ex, "UI Policy");
10 if (exceptionResult)
11 {
12 throw;
13 }
14 MessageBox.Show(string.Format("Failed to add word {0}, please contact support.", txtWordToCheck.Text));
15 }

Note: The method of throwing an exception is very important, not throw ex. If you throw ex, the stack exception will be replaced by a stack trace that throws the exception again, which is not expected

The following describes how to use the enterprise database configuration tool to configure the configuration file.

1. Right-click the App. config file. Select "Edit Enterprise Library V5 Configuration ". For example:

2. Manage program configuration exceptions. For example:

3. Click the coordinate arrow in the name column for the Default policy. In this way, the configuration tool automatically creates the Display Policy attributes. Change the property name to "UI Policy ". Note: The name here is the name parameter processed by HandleException in the try {} catch {} block. For example:

4. Add an exception handling policy. Right-click the title bar item "All except tions", click "Add Handlers", and click "Add Logging Exception Handler" in the context menu ". For example:

5. In this way, the log processing configuration section for exception handling configuration is automatically configured in the configuration file, and the "Logging Settings" configuration section is added for the log application block with the default configuration. Right-click "Logging Exception Handler", and the arrow on the left of the title bar shows the processed attributes. The default format is Text Exception Formatter, and the log information is recorded in the specified directory. The following is the [General] Directory. For example:

6. Save and close the application configuration.

Now, the basic configuration of the exception is complete. Next, run the program:

1. Enter a string with digits and click "Add Word. The running result is as follows:

2. View logs in the event viewer. Computer Management-Event Viewer-Windows logs-applications ]. For example:

Click an error log to view the exception details. The error message is as follows:

The above is the basic daily processing of the enterprise database. Of course, it also includes security and other information, which will be introduced later in the next section.

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.