Exception handling using Enterprise application block in 6 Steps
Exception handling is one of the most important tasks in any application. Many applications either do not handle applications or they handle it in an adhoc manner. In this section we will see how we can use the readymade exception handling block so that we do not need to code and build error handling routines from scratch.Please feel free to download my free 500 question and answer videos which covers Design Pattern, UML, Function Points, Enterprise Application Blocks, OOP'S, SDLC, .NET, ASP.NET, SQL Server, WCF, WPF, WWF, SharePoint, LINQ, SilverLight, .NET Best Practices @ these videos http://www.questpond.com/
Problem
A good error handling has the following features:- You would like to log errors in a source like event viewer, file etc for further debugging and inspection.
- You would also like to change these error logging sources on fly without compiling the project. So sometimes you like to see the error logged in event viewer and sometimes you would like to send an email for some critical errors to the administrators.
- You would also like to apply different error policies depending on where the error occurs. For instance if the error occurs on the UI you would like to throw the error on a label and if it happens on the data access layer you would like to log the error. For some errors you would like to replace it with some custom exception.
- Finally you would like to add, update and remove these error policies depending on situations on fly without compiling the code.
The Solution
The answer to the above problem is use the readymade block exception handling block.Step 1:- The first thing we need to do is add the logging application block. In case you have not read about logging application block please read about it atLogging.aspx. As we had mentioned previously one of the important aspects in error handling is that we should be able to log the errors in some source. This capability is provided by logging application blocks.

Step 2:- Now that we have defined where the errors should be logged it’s time to define exception policies. So right click and add exception handling application block and then right on the exception handling block to add exception policies as shown in below figure.





Specify the logging source for the logging handler as shown in below figure:

Select the formatter type as shown in below figure:


To define general exception right click on arithmetic exception and define the replace exception type as exception.

Below figure shows how we have define the replace exception type as general exception and a new exception message.

Step 6:- Now the final step calling the exception policy in the code. So first import the namespace i.e. exceptionhandling and logging in code.

exceptionpolicy static
class to handle the exception. You can see in the below figure we have two button one which uses policy1
and the other policy2
.
policy1
will be logged in to event viewer as shown in below figure.
policy2
will be replaced with a general error as shown in below figure.
No comments:
Post a Comment