Re: Multiple Catches in Try/Catch

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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.

<zacks@xxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:1137431762.315134.49950@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> 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?
>


.



Relevant Pages

  • 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: System.IO.Pipes.NamedPipeClientStream.Connect
    ... I was running the client pipe code on a ... Catch te As TimeoutException ... Catch ex As Exception ...
    (microsoft.public.dotnet.languages.vb)
  • debugger reports an handled exception as unhandled - in async methods
    ... I've got a problem with exception handling when using async methods. ... Actually, the code seems to runs fine, but the debugger reports that an ... BasicReturnCode res = DoSomeWork; ... TimeoutException can be thrown. ...
    (microsoft.public.dotnet.framework)
  • Re: Multiple Catches in Try/Catch
    ... > one catches the particular exception that was thrown, ... it executes the Finally block if it is present. ... > <log fatal error> ... > Exit Function ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to explicit trap a Time Out exception?
    ... Hello Mario, ... I am not quite sure of the .NET 1.x exception class if it meets your needs or not. ... College of Computing and Information Technology ... native exception such as TimeoutException or SqlTimeoutException? ...
    (microsoft.public.dotnet.framework.adonet)