Give me an example for wrapping an exception and Replacing an exception

Topics: Exception Handling Application Block
May 26, 2009 at 8:46 AM

Hi,

I need some code samples to implement wrapping an exception and also Replacing an Exception.

Please help me.

Thanks,

Ravi

May 26, 2009 at 8:55 AM
Edited May 26, 2009 at 8:56 AM

Have you checked the quickstart?   You wouldn't see the implementation in the code, those behaviors are defined the your application's configuration file (and the quickstart for ExceptionHandling provides a sample).  You can easily configure those things using the Entlib Configuration tool.  If you need more information on this, let me know.

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

May 26, 2009 at 10:07 AM

Hi Sarah,

Ihave done configuration and able to log the exception but my concern is implementing Exception handling in DAL and BLL.I will give u the code sample which  i followed in my project.

Even if exception occurs in BLL and DAL currently i am just logging the exception and rethrowing.Is it correct?

In the BLL Policy i have Logging Handler and Replace Handler.What Replace Handler is doing here.

In the DAL Policy i have defined Logging Handler and Wrap Policy.What actually Wrap handler is doing here in my code when exception occures in DAL Layer.

ExceptionPolicy has only Logging Handler.All handlers are of System.Exception

Please let me know.Please suggest me any changes if required.

UI Layer: 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

public

</font></font><font size="2" color="#0000ff">

 

</font>

partial class _Default : System.Web.UI.Page

{

<font size="2">

 

</font>

 

protected void Page_Load(object sender, EventArgs e)

{

 

}

<font size="2">

 

</font>

 

protected void Button1_Click(object sender, EventArgs e)

{

<font size="2">

 

</font>

 

try

{<font size="2">

 

</font>

 

int intOut = 0;<font size="2">

 

</font>

 

bool fs = int.TryParse("3", out intOut);<font size="2">

Label1.Text =

</font>

"Implementing Enterprise Library FlatFile" + intOut;

Label4.Text = Display.returnSomeText();<font size="2">

 

</font>

 

//int.Parse("TestLogger");

}

<font size="2">

 

</font>

 

catch (Exception ex)

{

<font size="2">

 

</font>

 

bool rethrow = ExceptionPolicy.HandleException(ex, "ExceptionPolicy");<font size="2">

 

</font>

 

if (rethrow)<font size="2">

{

</font>

 

throw; }

 

}

 

}

 

}

BLL Layer:

 

 

 

 

 

 

 

 

 

 

 

 

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

namespace

</font></font><font size="2" color="#0000ff">

 

</font>

SampleClsLib

{

<font size="2">

 

</font>

 

public class Display

{

<font size="2">

 

</font>

 

private const string EXCEPTION_POLICY = "BLL Policy";<font size="2">

 

</font>

 

public static string

returnSomeText()

{

<font size="2">

 

</font>

 

string retText="";<font size="2">

 

</font>

 

try

{

<font size="2">

retText = SampleClsLib.DAL.

</font>

 

DALDisplay

.returnSomeText();<font size="2">

 

</font>

 

int.Parse("BLL class");

}

<font size="2">

 

</font>

 

catch (Exception Ex)

{

<font size="2">

 

</font>

 

if (ExceptionPolicy.HandleException(Ex, EXCEPTION_POLICY)) throw;

}

<font size="2">

 

</font>

 

return retText+"Some Extra text is added in BLL Display Class";

}

}

}

DAL Layer:

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

namespace

</font></font><font size="2" color="#0000ff">

 

</font>

SampleClsLib.DAL

{

<font size="2">

 

</font>

class DALDisplay

{

<font size="2">

 

</font>

private const string EXCEPTION_POLICY = "DAL Policy";<font size="2">

 

</font>

public static string returnSomeText()

{

<font size="2">

 

</font>

string retText="";<font size="2">

 

</font>

try

{

<font size="2">

 

</font>

//int.Parse("Hi");

<font size="2">

retText =

</font>

"This Data is originated from DALDisplay Class";

}

<font size="2">

 

</font>

catch (Exception Ex)

{

<font size="2">

 

</font>

if (ExceptionPolicy.HandleException(Ex, EXCEPTION_POLICY)) throw;

}

<font size="2">

 

</font>

return retText;

}

}

}

May 26, 2009 at 10:39 AM

"Even if exception occurs in BLL and DAL currently i am just logging the exception and rethrowing.Is it correct?" - Are you asking if your approach is correct or if your code is correct?  I can't answer if it's the first as I don't know what is the requirement in your application.  If it's the latter,  what would happen I think is that when an exception occurs in the DAL layer, since you're wrapping it and rethrowing, it will be catched in the BLL and again, it will be logged and replaced as well.  Is that something you really intend to do? 

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

May 26, 2009 at 11:10 AM

I know that the code i used  is correct but here i want to  follow the guidlines  given in documentation.I am giving them here for reference

"exceptions that occur in the data access layer are logged and then wrapped inside another exception that provides more meaningful information to the calling layer. Within the business component layer, the exceptions are logged before they are propagated. Any exceptions that occur in the business component layer and that contain sensitive information are replaced with exceptions that no longer contain this information. These are sent to the user interface (UI) layer and displayed to the user."

"I think when an exception occurs in the DAL layer, since you're wrapping it and rethrowing, it will be catched in the BLL and again, it will be logged and replaced as well".

But  wrapping means we have to wrap the exception in a new exception,i am not doing this here.Can you tell me how i can do this?

I understood replacing an exception.

May 27, 2009 at 3:08 AM

Hi,

Basically, Wrapping an exception is just the same as configuring replacing exception. You would need to add the Wrap handler and select the type that will wrap exception. a detailed instruction can be found here http://msdn.microsoft.com/en-us/library/dd139928.aspx . Also pay attention on the Usage notes that can be found on the said link.

 

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com