Take Exception to everything
Wednesday, 04 August 2010
Article Index
Take Exception to everything
Catch clauses
Custom exceptions
Implementing exception safe code
ThreadException handling

Banner

A two-step process

The best way to think of implementing exception safe code in practice is probably to think of it as a two step - commit and rollback operation.

The try block attempts the commit and the catch implements the rollback.

In this scenario all of the objects have to be created before the try-catch block - otherwise the try and catch blocks couldn't operate on the same variables. This still leaves you open to some exceptions - for example running out of memory while creating objects.

There seems to be no way around the scope problems that try catch introduces.

Consider the response to a timeout exception for a moment. Perhaps what you want to do is give the user a chance to correct some data - a website address, say- and then try again.

This leads to the idea of Resume or Resume Next which are attractive propositions - until you realise the scope for misunderstanding its use could make, and has made, a mess of many an application.

Basic, but not VB .NET made Resume available and in unskilled hands it didn't work well and was the reason why VB .NET has structured exception handling.

However the basic response 

"something strange has happened where can we resume application execution" 

is still  reasonable..

Using try-catch you can create the same effect as Resume Next if you are prepared to do a lot of work. For example, suppose the correct result of a/0 is deemed to be 0 - don’t take this example too seriously in its detail - then you can "resume next" using:

int a=10;
int b=0;
int result;
try
{
result = a / b;
}
catch (DivideByZeroException)
{
result = 0;
}
MessageBox.Show(result.ToString());

Once again note that all the variables have to be defined before the try block. This just makes use of the basic behaviour of the try-catch - that execution continues after the last catch or the finally clause.

Notice that you can't protect multiple arithmetic statements or method calls that do the same arithmetic unless you are prepared to add try-catch around each one. In other words, there is no global way of resuming after an exception.

Many are of the opinion that such a thing would be a bigger exception waiting to happen but some just think that we haven't figured out how to implement such a facility in the right way.

If you want to resume, i.e. retry, the statement that caused the problem then this is really difficult. Using the same manual method you could simple write the statement out again:

try
{
result = a / b;
}
catch (DivideByZeroException)
{
b=2;
result = a/b;
}

but now the second attempt isn't protected.

You could use:

try
{
result = a / b;
}
catch (DivideByZeroException)
{
try { b = 2; result = a / b; }
catch (DivideByZeroException)
{
result = 0;
}
}
MessageBox.Show(result.ToString());

although now what to do in the inner try-catch is becoming a little contrived. It makes more sense to consider retrying in this way following a timeout or a file read error but the basic idea is sound and leads to constructions like:

bool flag=false;
do
{
try
{
result = a / b;
flag = true;
}
catch (DivideByZeroException)
{
result = 0;
flag = true;
}
} while (!flag);

You can think of this as try, put it right, and try again. In practice you would need to add something that terminated the loop when the retry had been attempted sufficiently often. There is also the problem of making sure that the try (or the catch) does not leave side effects that are accumulated with each repetition.

You can see that the try-try again pattern has the potential for making things worse as well as better.

Banner

<ASIN:0471081124>

<ASIN:0131857258>

<ASIN:0321694694>

<ASIN:0672330636>

<ASIN:0735626707>

<ASIN:0321658701>



Last Updated ( Wednesday, 04 August 2010 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.