Re: Try Catch Else Finally

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I guess I'll have to do some reading on finally. I'm still not sure I see the use. Perhaps it's how I'm using try catch. My purpose in using try catch is that without it, or some other error handling, if a command throws an exception the program halts with some garbage on the screen. I've written my program with try catch around statements like calls to other servers where something like a cable being cut might cause the line to throw and exception as it can not execute. In the catch I want to know what command is giving the problem. That'll help me know what is wrong. I print a custom message to indicate what code is being executed and the ex.message info to the screen for diagnosis. I need to know which stored procedure will not work, or is it the request to the remote server or did it try to parse the xml file and find out it wasn't a xml file? The presence of an error like this can mean skip the current record or it might mean reattempt the failed command until it starts working.

In any event after the try catch the program continues to run. I test a flag variable or some other way to see if the previous statement threw an exception and make my processing decisions based on that. So my code after the try catch always executes--how is that different from being in finally?

Cor Ligthert [MVP] wrote:
CJ,

The statements in the finally block will be always executed unless you power your computer down before that or execute the End statement. (Even if there is a return in the sub than the finally will be done first).

I hope this helps,

Cor

"cj" <cj@xxxxxxxxxxxxx> schreef in bericht news:O%23fbnudSGHA.4300@xxxxxxxxxxxxxxxxxxxxxxx
If I call a sub as one of the inside within a try, would the try trap errors in the sub? I hope not. My intention is only that this sub should only be run if there were no errors in the statement before it.

Also I'm not sure I understand Finally. How are statements in finally different from those that follow the end try?


Marina Levit [MVP] wrote:
Yes, that is what I am saying. That is the whole point of a try block.

You wouldn't know which one it was. You would need a separate try/catch for each line of code if you had different error handling code depending on which exact one threw the exception.
Or if the different things threw different types of exceptions, you could have multiple catch blocks each catching a different exception type, and have different error handling code execute that way.

"cj" <cj@xxxxxxxxxxxxx> wrote in message news:%23rn7ZedSGHA.4740@xxxxxxxxxxxxxxxxxxxxxxx
So are you saying if I have that if line 2 throws and exception it will skip 3, 4 and 5 and go straight to catch?

If so, the next question is how do I know it was line 2 that threw the exception and not line 1 or 4?

try
1
2
3
4
5
catch
a
end try

Marina Levit [MVP] wrote:
That's what the Try is for. To do stuff with the assumption that there are no errors. The catch is to deal with any errors. And the finally is to run regardless of whether or not there were errors.

"cj" <cj@xxxxxxxxxxxxx> wrote in message news:uOwfvRdSGHA.5552@xxxxxxxxxxxxxxxxxxxxxxx
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally.

Also because I understand Finally runs whether an error was caught or not, I haven't found a use for finally yet.



.



Relevant Pages

  • Re: Try Catch Else Finally
    ... Catch ex As Exception ... Nested try/catch blocks look pretty ugly and are hard to follow. ... The finally statements are different because they execute after the Try if no errors happened. ... Or if the different things threw different types of exceptions, you could have multiple catch blocks each catching a different exception type, and have different error handling code execute that way. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Try Catch Else Finally
    ... Sub Button1_ClickHandles ... ... ' Catch this exception because we know what to do if it occurs ... ' This will always execute, ... Cor Ligthert [MVP] wrote: ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Try Catch Else Finally
    ... If I call a sub as one of the inside within a try, would the try trap errors in the sub? ... Or if the different things threw different types of exceptions, you could have multiple catch blocks each catching a different exception type, and have different error handling code execute that way. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Try Catch Else Finally
    ... your computer down before that or execute the End statement. ... is a return in the sub than the finally will be done first). ... on which exact one threw the exception. ... have different error handling code execute that way. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Try Catch Else Finally
    ... your catching different types of 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. ... Sub Button1_ClickHandles ... ... ' This will always execute, ...
    (microsoft.public.dotnet.languages.vb)