|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Multiple Catches in Try/Catchone 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? za***@construction-imaging.com wrote:
> The help isn't clear on this. If I have multiple catches and the first from the docs (emphasis added):> one catches the particular exception that was thrown, does it check the > rest of the catches in the try/catch? 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.*** > ' now execution jumps to a Finally block (if present) or to just after> 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> End Try (if not) > Catch ex as Exception No.> <log fatal error> > Exit Function > End Try > > Or do I really need a Exit Try in the TimeoutException's Catch? -- Larry Lard Replies to group please 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? > <za***@construction-imaging.com> schrieb:
> Try I wonder why you do not check this out yourself...> 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? -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/>
Display the contents of a folder in a listbox?
How do I extract a page from word and insert into a new word document using VB Image dimensions? How to convert a selectedIndex to SelectedValue for ComboBox ms.public.dotnet.vb.general - an "ex group" Append to XML? Enter key vs Return key Comm Ports Question on VB.Net security for the application to run on network drive Code for Convert RTF Text to HTML |
|||||||||||||||||||||||