Enterprise Library Exception Handling Application Block - adding context to exceptions


C

clintonb

When using the Enterprise Library Excption Handling Application block,
how can I pass extra contextual information to the Exception Handling
block so I can make more user-friendly exceptions.

Here is an example where I'd like to add contextual info. I have a 3rd
party report engine that throws exceptions. I'd like to wrap that
exception in a more user-friendly exception. If I open a report file
that I assume exists, and it doesn't exist, the ReportEngine will throw
an exception. But the message is too generic and not the most
user-friendly. I'd like to wrap it in some new exception that would
have some message that says: "Unable to open report file
'SomeReport.rpt'".

The problem is, to use the exception handling block, you call
ExceptionPolicy.HandleException(), but the only parameters are the
exception and the policy name. It seems like the message of the
wrapping exception is "hard coded" in the configuration file.

Have any of you users of the Exception Handling Application Block tried
to do something similar to what I want? If so, how did you handle it.
Thanks.

try
{
ReportEngine engine;
engine.OpenReport("SomeReport.rpt");
}
catch( ReportEngine.FileNotFoundException ex )
{
// Notice the next line doesn't allow you to provide contextual info.
bool rethrow = ExceptionPolicy.HandleException(ex, "Some Policy");
if (rethrow)
{
throw;
}
}
 
Ad

Advertisements

A

Alvin Bruney

You don't have to, you can throw your own exception derived objects and it
will still be caught by the enterprise library.

--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
 
C

clintonb

Alvin,
Thanks for the reply.

But I'm a little confused by your response. The catch block in the
sample I gave could throw my own exceptions, but I want a exception
handling policy to do that for me.

I was hoping to call a "report exception" handling policy that would
wrap the 3rd party report engine message and wrap it in one of my
user-friendly exceptions. But to make a user-friendly message, I
wanted to provide more info. In my example, I wanted to pass along the
report path.

- Clint


Alvin said:
You don't have to, you can throw your own exception derived objects and it
will still be caught by the enterprise library.

--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------

When using the Enterprise Library Excption Handling Application block,
how can I pass extra contextual information to the Exception Handling
block so I can make more user-friendly exceptions.

Here is an example where I'd like to add contextual info. I have a 3rd
party report engine that throws exceptions. I'd like to wrap that
exception in a more user-friendly exception. If I open a report file
that I assume exists, and it doesn't exist, the ReportEngine will throw
an exception. But the message is too generic and not the most
user-friendly. I'd like to wrap it in some new exception that would
have some message that says: "Unable to open report file
'SomeReport.rpt'".

The problem is, to use the exception handling block, you call
ExceptionPolicy.HandleException(), but the only parameters are the
exception and the policy name. It seems like the message of the
wrapping exception is "hard coded" in the configuration file.

Have any of you users of the Exception Handling Application Block tried
to do something similar to what I want? If so, how did you handle it.
Thanks.

try
{
ReportEngine engine;
engine.OpenReport("SomeReport.rpt");
}
catch( ReportEngine.FileNotFoundException ex )
{
// Notice the next line doesn't allow you to provide contextual info.
bool rethrow = ExceptionPolicy.HandleException(ex, "Some Policy");
if (rethrow)
{
throw;
}
}
 
Ad

Advertisements

A

Alvin Bruney

The exception handling policy in the Enterprise library is for catching
exceptions. The framework is not concerned with throwing exceptions. So
basically, you wrap your exceptions however you want to wrap it and provide
what ever message you feel is appropriate and then throw it. Then you tweak
the aspects container to catch that type of exception and do something with
it.

--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------

Alvin,
Thanks for the reply.

But I'm a little confused by your response. The catch block in the
sample I gave could throw my own exceptions, but I want a exception
handling policy to do that for me.

I was hoping to call a "report exception" handling policy that would
wrap the 3rd party report engine message and wrap it in one of my
user-friendly exceptions. But to make a user-friendly message, I
wanted to provide more info. In my example, I wanted to pass along the
report path.

- Clint


Alvin said:
You don't have to, you can throw your own exception derived objects and
it
will still be caught by the enterprise library.

--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------

When using the Enterprise Library Excption Handling Application block,
how can I pass extra contextual information to the Exception Handling
block so I can make more user-friendly exceptions.

Here is an example where I'd like to add contextual info. I have a 3rd
party report engine that throws exceptions. I'd like to wrap that
exception in a more user-friendly exception. If I open a report file
that I assume exists, and it doesn't exist, the ReportEngine will throw
an exception. But the message is too generic and not the most
user-friendly. I'd like to wrap it in some new exception that would
have some message that says: "Unable to open report file
'SomeReport.rpt'".

The problem is, to use the exception handling block, you call
ExceptionPolicy.HandleException(), but the only parameters are the
exception and the policy name. It seems like the message of the
wrapping exception is "hard coded" in the configuration file.

Have any of you users of the Exception Handling Application Block tried
to do something similar to what I want? If so, how did you handle it.
Thanks.

try
{
ReportEngine engine;
engine.OpenReport("SomeReport.rpt");
}
catch( ReportEngine.FileNotFoundException ex )
{
// Notice the next line doesn't allow you to provide contextual info.
bool rethrow = ExceptionPolicy.HandleException(ex, "Some Policy");
if (rethrow)
{
throw;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top