M
Mark
Apologies for the lengthy posting....
In a previous posting:
http://groups.google.com/groups?hl=...%240%2422605%24cc9e4d1f%40news.dial.pipex.com
I asked a question about how to ensure that some "tidying up" code would be
run at the end of a Method independently of where the method exits. The
consensus was to use try-finally blocks and using blocks: In the former
case, I could put all my "tidying up" code in the finally block and
everything else in the try block. In the case of simply disposing, I could
initialise in a "using" block.
Whilst this mostly met the requirements, I ran into two issues:
1. Putting large amounts of code in the try block is OK and any return
statements therein do indeed cause the code in the finally block to run.
However, the presence of the try block causes any code that would normally
generate an unhandled exception (invariably some buggy code that I've
written!) to keep quiet. In general, I like to handle only the exceptions
that I choose. The rationale being that any other kind of exception I'd
rather know about and fix the bug, which it invariably is. So, wrapping up
great chunks of code into a try block seems to be out of the question.
2. Of course, the using block only disposes of objects that are initialised
in the using statement. However, an example of something I want to happen
just before the Method exists is writing to a TraceListener something like
"Method is now exiting...".
As I see it, there are three solutions:
1. Use deeply nested (i.e. large numbers of) if and switch statements and
simply put the "tidy up" and trace code at the end as one normally would. I
don't like this because the I end up with huge amounst of nested code.
2. Add the tidy up and trace code before each return statement and also at
the end. This keeps the nesting down, but does cause a lot of repetion
within the method.
3. Use the evil(ish) goto! That is, simply add a goto TidyUpStuff and put
all the tidying up and trace code after the TidyUpStuff label which would be
at the end of the method. I'm not an anti-goto Zealot, but I haven't used
goto since the early days of basic and would rather not revisit it if I
don't have to.
Any thoughts?
Cheers
Mark
In a previous posting:
http://groups.google.com/groups?hl=...%240%2422605%24cc9e4d1f%40news.dial.pipex.com
I asked a question about how to ensure that some "tidying up" code would be
run at the end of a Method independently of where the method exits. The
consensus was to use try-finally blocks and using blocks: In the former
case, I could put all my "tidying up" code in the finally block and
everything else in the try block. In the case of simply disposing, I could
initialise in a "using" block.
Whilst this mostly met the requirements, I ran into two issues:
1. Putting large amounts of code in the try block is OK and any return
statements therein do indeed cause the code in the finally block to run.
However, the presence of the try block causes any code that would normally
generate an unhandled exception (invariably some buggy code that I've
written!) to keep quiet. In general, I like to handle only the exceptions
that I choose. The rationale being that any other kind of exception I'd
rather know about and fix the bug, which it invariably is. So, wrapping up
great chunks of code into a try block seems to be out of the question.
2. Of course, the using block only disposes of objects that are initialised
in the using statement. However, an example of something I want to happen
just before the Method exists is writing to a TraceListener something like
"Method is now exiting...".
As I see it, there are three solutions:
1. Use deeply nested (i.e. large numbers of) if and switch statements and
simply put the "tidy up" and trace code at the end as one normally would. I
don't like this because the I end up with huge amounst of nested code.
2. Add the tidy up and trace code before each return statement and also at
the end. This keeps the nesting down, but does cause a lot of repetion
within the method.
3. Use the evil(ish) goto! That is, simply add a goto TidyUpStuff and put
all the tidying up and trace code after the TidyUpStuff label which would be
at the end of the method. I'm not an anti-goto Zealot, but I haven't used
goto since the early days of basic and would rather not revisit it if I
don't have to.
Any thoughts?
Cheers
Mark