Multiple Catches in Try/Catch

Z

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?
 
L

Larry Lard

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.
 
A

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.
 
H

Herfried K. Wagner [MVP]

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Problem with Try Catch Block 2
Try Catch 5
Try ... Catch Loop 7
Try Catch Statement help 4
Help with Try/catch 4
Exception Catch but dont throw 6
Try/Catch question 8
try\catch getting the right line number 4

Top