Re: Multiple Catches in Try/Catch

Tech-Archive recommends: Fix windows errors by optimizing your registry




zacks@xxxxxxxxxxxxxxxxxxxxxxxx wrote:
> The help isn't clear on this. If I have multiple catches and the first
> one catches the particular exception that was thrown, does it check the
> rest of the catches in the try/catch?

from the docs (emphasis added):

If an exception occurs, Visual Basic examines the Catch statements in
the order they appear within Try...Catch...Finally. If it finds a Catch
statement that handles the generated exception, it executes ***the
corresponding statement block. When it has finished executing the Catch
block, it executes the Finally block if it is present. Execution then
proceeds to the statement following the End Try statement.***


>
> For example, I want to execute a SQL command and if it times out I want
> to warn the user and continue. If any other error occurs I want to
> treat it as a "fatal" error,
>
> Try
> cmd = New SqlCommand("some sql code", connection)
> cmd.CommandTimeout = 120
> cmd.ExecuteNonQuery
> Catch ex as TimeoutException
> <warn the user that a timeout occured>

' now execution jumps to a Finally block (if present) or to just after
End Try (if not)

> Catch ex as Exception
> <log fatal error>
> Exit Function
> End Try
>
> Or do I really need a Exit Try in the TimeoutException's Catch?

No.

--
Larry Lard
Replies to group please

.



Relevant Pages

  • Re: Finally in IE
    ... If an exception is thrown, each catch clause is inspected in turn ... the clause executes wether or not an exception is thrown ... Firefox considers your <script> block as self-enclosed system. ...
    (comp.lang.javascript)
  • SqlDataAdapter.Fill returns results of previous command
    ... This code executes successfully at certain points in the application, ... I don't know what the cause of this exception is yet, ... stored procedure was indeed executed on the database. ... The trace shows that the last action on the db was the stored ...
    (microsoft.public.dotnet.framework.adonet)
  • Multiple Catches in Try/Catch
    ... I want to execute a SQL command and if it times out I want ... Catch ex as Exception ... Exit Function ...
    (microsoft.public.dotnet.languages.vb)
  • Re: try...finally is more powerful than I thought.
    ... When no exception occurs, the finally clause is executed. ... or executes a return or break statement, ...
    (comp.lang.python)
  • Re: try...finally is more powerful than I thought.
    ... When no exception occurs, the finally clause is executed. ... >or executes a return or break statement, ... (The reason is a problem with the current implementation -- ...
    (comp.lang.python)