Exception Handling With using Exception Policy??

Topics: Enterprise Library Core, Exception Handling Application Block, Logging Application Block, Policy Injection Application Block
Oct 13, 2008 at 4:50 AM
Is it possible to use the Exception handling block and pass all exception to Logging Application Block to log the problem without using "try catch" and following code?

ExceptionPolicy.HandleException(ex, "Logging Policy");

Or we still need to define "try catch" under every functions?

Oct 13, 2008 at 1:18 PM
Hi,

Yes, you need to catch exceptions at some point. You can create some helper method taking a delegate and a policy name to avoid writing the try/catch every time, though.

Fernando
Oct 13, 2008 at 1:32 PM
Thanks Fernando

BTW, any reference to create the helper method??

Alex
Oct 13, 2008 at 5:17 PM
Something like this should help

        public static void RunAndHandleExceptions<T>(Action action, string policyName)

            where T : Exception

        {

            // null checks for action and policyName here

 

            try

            {

                action();

            }

            catch (T exception)

            {

                if (ExceptionPolicy.HandleException(exception, policyName))

                {

                    throw;

                }

            }

        }



Which you can invoke doing

 

            RunAndHandleExceptions<SqlException>(

                () =>

                {

                    // do something here

                    int foo = 1 / zero;

                },

                "myPolicy");