Click here to Skip to main content
Email Password   helpLost your password?
Updated with links of Other Application Blocks

Introduction

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.

I have been writing and recording videos on design pattern, UML, estimation and lot more, you can see my work here.

Other Application Blocks

Validation application blocks: - This article explains the 16 steps you need to perform to do validations using VAB.Validation application blocks

Client side validation: - One of the short comings in VAB is that it does only server side validations. This article talks how we can leverage VAB for client side.Client side validation

Dynamic validation: - This article explains how to build dynamic validation on scenario basis.Dynamic validation

Policy Application blocks: - This article talks how to implement plug and play mechanism using Policy application blocks.Policy application block

Logging application block: - This article explains the 5 basic steps of how to use logging application blocks.
Logging application block

Data application: - This article talks about the four steps you need to implement data application blocks.
Data application block

Unity application block: - This application talks about Unity Application Block in DI and IOC.Unity application block

UIP block: - This article talks about Reusable Navigation and workflow for both Windows and Web using Microsoft UIP blocks.UIP block

Problem

A good error handling has the following features:

So what is the problem? If we want to build the above features from scratch it’s a project by itself.

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 at Questpond1.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.

We will give a name to this policy as ‘Policy1’.

Step 3:- Now right click on policy 1 and define a new exception type as shown in below figure.

For the current scenario select the exception type as general .NET exception as shown in below figure. This type specifies that the policy will catch this type of exception.

Step 4 :- In case this exception occurs we would need to direct the exception block what should be done. For policy exception we would like to log the same in the logger. So click on the exception defined in policy 1 and say that this needs to be logged using the logging handler.

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

Select the formatter type as shown in below figure:

Step 5:- Let’s make this example more interesting to exploit the real essence of exception handling block. Let’s add one more policy called as policy 2. In policy 2 we will catch the arithmetic exception. This arithmetic exception will be replaced by general exception.

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.

Click to enlarge

So we have defined two policy one policy i.e. policy1 will take the exception and log it in the event viewer and the other policy will take a arithmetic exception and replace it with a general exception and throw it to the caller.

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.

Finally use the 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.

Click to enlarge

The output of policy1 will be logged in to event viewer as shown in below figure.

The output of policy2 will be replaced with a general error as shown in below figure.

The best part of exception handling block is you can change policy on fly without compiling the code. You can also change the error logging source from event viewer to file or email. I hope you have enjoyed this article and I am sure if you use this block properly you can have a very stable, efficient and flexible error handling framework.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralI like your article
filotas
9:26 9 Nov '09  
It's brief and direct.

It gave me the starting point to continue researching by myself.

Thanks
GeneralGood introduction
Donsw
10:19 20 Apr '09  
I guess you are going to use one application block Microsoft wants you to use them all. they seem very intertwined.

cheers,
Donsw
My Recent Article : Ajax Calendar Control

GeneralMy vote of 1
Orvindo
16:00 27 Mar '09  
Yoy have updated the article. What are the changes?
GeneralRe: My vote of 1
Danila Korablin
23:16 28 Mar '09  
I think you can see all changes here:
http://www.codeproject.com/script/Articles/ListVersions.aspx?aid=30299[^] (Press the compare button Wink )

Wormhole is the God divided by zero

GeneralI find your article very helpful but...
devnet247
12:56 21 Oct '08  
Thanks for your time in writing these articles however they are never backed up by code,and very very simplistic and not applicable to real world.

I see you did few articles on enterprise library.
Why dont you put an example together about caching and how you would approach caching in a hybrid framework used by both windows and web forms
and what caching strategy you would adopt in the middle tier to accomodate both windows and web app.

again
thanks for posting so many articles.

thanks a lot

QuestionObjection
predragzakisevic
8:28 21 Oct '08  
C# (C# 1.0, C# 2.0, C# 3.0, C#), VB (VB 7.x, VB 8.0, VB 9.0, VB 6, VB), SQL, .NET (.NET, .NET 3.5, .NET 3.0, Mono, DotGNU, .NET 1.0, .NET 1.1, .NET 2.0), SQL Server (SQL 2000, SQL 2005, SQL CE, SQL Server), Visual Studio (VS.NET2002, VS.NET2003, VS2005, VS6, VS2008, Visual Studio), ASP, ASP.NET, ADO, ADO.NET, Ajax, CEO, Architect

Why does a CEO needs to know all these ? Give me a break.
AnswerRe: Objection
spoodygoon
8:44 21 Oct '08  
Shivprasad koirala puts a lot of effort into sharing knowledge and I respect that, but this adding all the tags thing that seems to becoming habit is really not helpful. It really prevents the filters from being worth a darn.
GeneralRe: Objection
Shivprasad koirala
8:52 21 Oct '08  
Updated and noted.Its my mistake

When i die i die programming

GeneralWhy are you documenting someone else's technology?
PIEBALDconsult
4:52 21 Oct '08  
Why are you documenting someone else's technology?
GeneralRe: Why are you documenting someone else's technology?
Shivprasad koirala
4:57 21 Oct '08  
Code project is all about making good article of some one else's technology and the above article is of microsoft technology. Enterprise blocks helps us to speed up project schedule. Smile I am not so great to make my own technology Smile

When i die i die programming

GeneralRe: Why are you documenting someone else's technology?
PIEBALDconsult
5:03 21 Oct '08  
No it isn't; it's about sharing what your own code.

If Microsoft creates something, they document it.
If you write something; you document it.
GeneralRe: Why are you documenting someone else's technology?
Shivprasad koirala
6:01 21 Oct '08  
Oh i think now i understand why the name code project. I will try to post some of my own code. But i just thought i should share this thing..

When i die i die programming

GeneralRe: Why are you documenting someone else's technology?
Nicola Tufarelli
6:32 21 Oct '08  
Shivprasad koirala wrote:
But i just thought i should share this thing

I agree with you; sharing knowledge with others is always a great thing, even if it's all about a sample over someone else's work.
A simple walkthrough maybe of help for someone.
GeneralRe: Why are you documenting someone else's technology?
Shivprasad koirala
6:02 21 Oct '08  
The other thing is i have seen so many articles there which talks about products and are on code project. So even i walked on the same path.

When i die i die programming

GeneralRe: Why are you documenting someone else's technology?
Alomgir Miah A
7:25 30 Mar '09  
Very useful articles. Dont worry about these buggers. You are doing great.
GeneralRe: Why are you documenting someone else's technology?
rperetz
6:37 12 Jan '09  
Give me a break with your rules.
If someone want to share something he did, even if based on Microsoft so be it.
do you want people not to post code on WF or WCF? it's also Microsoft tech no?
are you happy in your life?
GeneralRe: Why are you documenting someone else's technology?
Billou_13
1:33 26 Mar '09  
Personally, I found this article very usefull.
Thanks to you for sharing it !!! Big Grin
GeneralRe: Why are you documenting someone else's technology?
Holfling
2:31 23 Dec '09  
I also found it useful.

For a quick overview of the Exception handling Enterprise application block I can either
a) Wade through pages and pages of MSDN material.
b) Read through your concise and useful article.

I know which I'd choose. Don't take any notice of the naysayers...
GeneralUse an if statement
Mark Nischalke
2:03 21 Oct '08  
You should wrap the ExceptionPolicy.HandleException in an if statement so it can handle the PostHandlingActions None and Rethrow

if(ExceptionPolicy.HandleException(ex, "Policy Name"))
throw;

In this case if the PostHandlingAction is Rethrow the exception will be handled then rethrown up the chain.


only two letters away from being an asset

GeneralRe: Use an if statement
Shivprasad koirala
6:03 21 Oct '08  
Thats a good thought rather i will say thats way it should be done. Thanks

When i die i die programming


Last Updated 26 Mar 2009 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010