Home All Groups Group Topic Archive Search About

Multiple Catches in Try/Catch

Author
16 Jan 2006 5:16 PM
zacks
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?

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>
Catch ex as Exception
   <log fatal error>
   Exit Function
End Try

Or do I really need a Exit Try in the TimeoutException's Catch?

Author
16 Jan 2006 5:26 PM
Larry Lard
za***@construction-imaging.com 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
Author
16 Jan 2006 5:29 PM
AMDRIT
No,

the catch will fall until it finds a match, then the catch routine is
completed.  The preferred way to catch exceptions is to go from most like
(Specific) to least likely (Unspecific)

try
catch(ex as mycustomexpection)
  'We threw this one ourselves, its probably recoverable
catch(ex as TimeoutException)
  'We took too long, but probably recoverable
catch(ex as applicationexception)
  'Unexpected exception, in the application
  'Report, log, and ask to stop
catch(ex as system.exception)
  'Unexpected exception, not in the application
  'Report, log and stop
finally
   'Perform and ultimate clean up here
end try

Hope that helps.

<za***@construction-imaging.com> wrote in message
Show quoteHide quote
news:1137431762.315134.49950@g49g2000cwa.googlegroups.com...
> 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?
>
> 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>
> Catch ex as Exception
>   <log fatal error>
>   Exit Function
> End Try
>
> Or do I really need a Exit Try in the TimeoutException's Catch?
>
Author
16 Jan 2006 6:07 PM
Herfried K. Wagner [MVP]
<za***@construction-imaging.com> schrieb:
> Try
>    cmd = New SqlCommand("some sql code", connection)
>    cmd.CommandTimeout = 120
>    cmd.ExecuteNonQuery
> Catch ex as TimeoutException
>   <warn the user that a timeout occured>
> Catch ex as Exception
>   <log fatal error>
>   Exit Function
> End Try
>
> Or do I really need a Exit Try in the TimeoutException's Catch?

I wonder why you do not check this out yourself...

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>