I agree with your points and recommendations, and I would never want to
write a bunch of nested try... catch finally blocks. I was primarily
interested in discovering the alternatives in .NET/C# SEH to Resume and
Resume Next. Apparently there are none in C# and I can live with that. I'm
just a bit surprised that the show pretty much has to be over when you run
into an error. That is, one apparently cannot have logic that, when an
exception is encountered, can continues on it's happy way if the
error-handling logic determines that proceeding is okay. So, for example if
I have code that's doing a bunch of important tasks, and in the middle of
those tasks it goes to write some unimportant-but-interesting things to a
log file; if the log file cannot be written to (and an exception is thrown),
then I cannot, in C#, have logic that allows the important tasks to finish
even though an unimportant task of writing to the log file caused an
exception. Of course I could break up the logic into separate methods - but
short of doing that, there are apparently no good options for proceeding
after an exception is thrown. Okay.
-v
Andreas Håkansson said:
Verde,
I would not recommend you writing code like this, first of all there are
some
problems with memory leaks [1], but you should write it like this (note this
is
just an example and not a a guideline to which exceptions you always should
try to catch)
try
{
// A couple of lines of code, each which is
// able to throw an exception.
}
catch(SecurityException a)
{
// Catch any security exceptions
}
catch(ArgumentNullException b)
{
// Catch wrong arguments
}
catch(Exception c)
{
// When all else fails, catch the sucker
// anyway =)
}
[1]
http://support.microsoft.com/default.aspx?scid=kb;en-us;810178
HTH,
//Andreas
Verde said:
That is what your finally block is for. You try something, you
catch
or regardless lines
of code
in