SharpCrafters Forum – extension of exception handling

SharpCrafters Support Forum

        


extension of exception handling Expand / Collapse
Author
Message
Posted 12/15/2008 2:05:48 PM


Community Member
Hello,

I would like to make two new "aspects" which are very connected with exceptions handling.

First one would be good for UI scenario, when I want to allow a user to decide repeat or stop the action in case of exception. The code might look like this

bool repeat = false;
do
{
   try
   {
       // itself code
       ExportLoop(ExportSettings);

   }
   // different catches, block OnException(MethodExecutionEventArgs eventArgs)
   catch (System.Data.SqlClient.SqlException ex)
   {
       if (MessageBox.Show("An error ocured, try again?", "Error", MessageBoxButtons.YesNo) == DialogResult.Yes)
           repeat = true;
       else
           repeat = false;
   }
} while (repeat);

It seems to me like a new FlowBehavior.Repeat, am I rigth?


The second scenario is for tasks (e.g. network access) which are supposed to repeat X times with Y delay when they are not succesfull. E.g. server is not found, try it for 5 times with 1 second delay.

int delay = 1000; // ms
int left = 5;
while (left>0)
{
   try
   {
       // itself code
       ExportLoop(ExportSettings);
       repeat = 0;
   }
   // different catches, block OnException(MethodExecutionEventArgs eventArgs)
   catch (System.Data.SqlClient.SqlException ex)
   {
           repeat--;
           Thread.Sleep(delay); // not sure it is the best way how to delay thread
   }
}


Is there already any way how to do it with PostSharp right know? How? I haven't look into PostSharp.Core yet. I assume I can't to connect On Entry and On Exit handlers together like (pseudocode):
On Entry (...)
{/*int delay = 1000; int left = 5;do{ */}
On Exit  (...)
{/*}while (left>=0);*/}

Is it possible to include it into PostSharp? I can, of course, contribute on it, with testing for example.

Provided code is just an idea, it is not tested.

Jirka Nouza
Post #3424
Posted 12/15/2008 4:53:57 PM


Gael Fraiteur

SharpCrafters
Jirko,

Thank you for your suggestion.

It's a good idea to add the "Repeat" flow behavior.
I would advise to use OnMethodInvocationAspect to implement it using current features.

-gael
Post #3425
« Prev Topic | Next Topic »


All times are GMT +1:00, Time now is 11:42am

Powered By InstantForum.NET v4.1.4 © 2010
Execution: 0.017. 7 queries. Compression Disabled.