Try...Catch question

J

jy836

When using the Try...Catch...Finally statements, what happens if an error
occurs in a procedure called by the main procedure? For instance...

Private Sub DoSomething()

Try
Call DoSomethingElse()
Catch
MsgBox("An error occured.")
Finally
MsgBox("This code always executes.")
End Try

End Sub

If an error occurs in the DoSomethingElse procedure, will the error be
caught in the Try...Catch...Finally exception handler?
 
S

Scott M.

Usually yes, the procedure that is called (DosomethingElse()) would "bubble"
its exception back to the Try block.
 
C

Cor

Hi Jy

In addition to Scott,

Only not if in that Call is something that "end" or on another way kills the
program.

Cor
 
C

Charles Law

Further to the other answers, if DoSomethingElse() contains its own
Try..Catch block, it is possible to catch the exception and return without
causing the exception to be caught at the outer level. It depends on how you
want it to work.

HTH

Charles
 
H

Herfried K. Wagner [MVP]

* "jy836 said:
When using the Try...Catch...Finally statements, what happens if an error
occurs in a procedure called by the main procedure? For instance...

Private Sub DoSomething()

Try
Call DoSomethingElse()
Catch
MsgBox("An error occured.")
Finally
MsgBox("This code always executes.")
End Try

End Sub

If an error occurs in the DoSomethingElse procedure, will the error be
caught in the Try...Catch...Finally exception handler?

Yes, if it's not caught in the 'DoSomethindElse' procedure or if it's
thrown by this procedure.
 

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


Top