C
Charles Law
Hi Cj
The Catch in Button1_Click() catches any exception that 'escapes' from
DoFileStuff(). So, if UnexpectedException is thrown it will be caught in
Button1_Click(). If SomeException is thrown it will be caught in
DoFileStuff(). If DoFileStuff() rethrows it, or throws it as some other
exception, then that will be caught in Button1_Click(). Otherwise,
DoFileStuff() can consume the exception and retry the offending code or move
on to the next iteration, if that is what is happening (the logic in
DoFileStuff() would have to support this).
I wasn't trying to be prescriptive about how your exception handling should
be done. I was just trying to give an example of how Finally is useful.
However, there does seem to be a case for catching some exceptions
separately, and then having a catch-all to cover unexpected exceptions. You
could have
<code>
Sub DoFileStuff()
Try
OpenFile
WriteToFile
Catch ex As SpecificException
' Catch this exception because we know what to do if it occurs
Catch ex As SomeOtherExceptionThatICanHandle
' Catch this exception as well for the same reason. However
' it requires a different reaction, so we catch it separately
Catch ex As Exception
' Catch all other exceptions and perform some
' default action and try to keep going
Finally
If FileIsOpen Then
CloseFile
End If
End Try
End Sub
</code>
Charles
1. is the button1_click catch catching exceptions thrown by lines inside
dofilestuff or just if the line dofilestuff itself for some reason threw
an exception?
The Catch in Button1_Click() catches any exception that 'escapes' from
DoFileStuff(). So, if UnexpectedException is thrown it will be caught in
Button1_Click(). If SomeException is thrown it will be caught in
DoFileStuff(). If DoFileStuff() rethrows it, or throws it as some other
exception, then that will be caught in Button1_Click(). Otherwise,
DoFileStuff() can consume the exception and retry the offending code or move
on to the next iteration, if that is what is happening (the logic in
DoFileStuff() would have to support this).
2. your catching different types of exceptions. I'm catching any
exceptions and treating them all the same. The show must go on in my
program regardless of any exceptions. Some lines an exception on them
would be best handled by retrying the line and others by skipping to the
next iteration of the program. ie on reading from a server it'd be best
to retry untill it works while on getting a badly formated xml file from
the server I'd probably flag that transaction as questionable and try the
next request.
I wasn't trying to be prescriptive about how your exception handling should
be done. I was just trying to give an example of how Finally is useful.
However, there does seem to be a case for catching some exceptions
separately, and then having a catch-all to cover unexpected exceptions. You
could have
<code>
Sub DoFileStuff()
Try
OpenFile
WriteToFile
Catch ex As SpecificException
' Catch this exception because we know what to do if it occurs
Catch ex As SomeOtherExceptionThatICanHandle
' Catch this exception as well for the same reason. However
' it requires a different reaction, so we catch it separately
Catch ex As Exception
' Catch all other exceptions and perform some
' default action and try to keep going
Finally
If FileIsOpen Then
CloseFile
End If
End Try
End Sub
</code>
Charles