Exception Handling and return "system error value"

Topics: Exception Handling Application Block, General discussion, Logging Application Block
May 22, 2008 at 3:44 AM
Hi all

I have defined an exception handling policy to trap all exception and will pass it to logging Application block to log it to event log.

It is fine.

But is it ok to add one more action that I could return a null value to the parent function.

Now, when I use

try
{
 return returnvalue;
}
catch
{
   return "System error occurred";
}

It couldn't be logged in the event log. Could I make the program still log it to event log and return the error message?




May 22, 2008 at 5:44 PM
Hi,

I'm not sure I understand what you want to accomplish. The snippet you are showing will never get to the catch block, assuming returnvalue is a variable and not an expression. And you're not using the EHAB there. Can you elaborate on what you need?

Fernando


hnchan wrote:
Hi all

I have defined an exception handling policy to trap all exception and will pass it to logging Application block to log it to event log.

It is fine.

But is it ok to add one more action that I could return a null value to the parent function.

Now, when I use

try
{
 return returnvalue;
}
catch
{
   return "System error occurred";
}

It couldn't be logged in the event log. Could I make the program still log it to event log and return the error message?







May 23, 2008 at 3:12 AM
Thanks Fernando

Here is what I want to do .

try
{
 return 1/0;
}
catch
{
   return "System error occurred";
}

Under the web.config. I have configed the Excpetion handler will pass the error to logging handler to record the error detail under event log.
However,  if I use the above code, it just will return "System error occurred" to my program but not trapped the error detail.

Is it possible to implement a solution support logging the error to event log and return my desire message, Thanks?
May 23, 2008 at 2:12 PM
Hi,

You'd use the EHAB when you want to determine through configuration what to with exceptions. If you know you want to return a value then using the EHAB may not be the way to go (but other factors like logging only certain exceptions may still make it worthwhile).

You can always log in the catch block without going through EHAB. You can also call EHAB but ignore its recommendations to throw the exception; you'd use the HandleException overload that doesn't throw exceptions and returns the exception to throw instead. Whether this is convenient or not depends on your scenario; my guess is it's not and you would be better off just logging the exception yourself.

Fernando



hnchan wrote:
Thanks Fernando

Here is what I want to do .

try
{
 return 1/0;
}
catch
{
   return "System error occurred";
}

Under the web.config. I have configed the Excpetion handler will pass the error to logging handler to record the error detail under event log.
However,  if I use the above code, it just will return "System error occurred" to my program but not trapped the error detail.

Is it possible to implement a solution support logging the error to event log and return my desire message, Thanks?