What does Try, Catch ex As Exception, and End Try do?

G

garyusenet

Hi I'm using the following code which is finally working.

Public Class Form1

Shared ActElement As Object
Shared ActFields As DataSet


Public Sub SetActElement()
Dim objApp As New Object


Try


objApp = CreateObject("actole.database")
objApp.OpenEx("")
If objApp.IsOpen = False Then
MessageBox.Show("Failed to connect to ACT")
Application.Exit()
End If
ActElement = objApp.contact

' get the database path
' MsgBox(objApp.GetDataBasePath)

ActElement.Add()
ActElement.Data(26, "A SuperStar")
ActElement.Update()

Catch ex As Exception

End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
SetActElement()
End Sub
End Class

I understand most of what's going on but i've never met Catch, or Try
before - i've had a look at microsofts site and couldn't make head nor
tail of it - can someone explain in beginners term, what these words
are used for. And specifically in the context of my little program what
they are doing?

Thanks

Gary.
 
N

n4ixt

If an error occurs in your app after the TRY point, the code will jump to
the CATCH section. For example, let's say your create object call failed,
code would jump in to the Catch area. You could then add code that displayed
the error message that's contained in the EX object. Something like:
Catch ex As Exception

MsgBox("Error has occurred: " & ex.Message)

Or, you could throw the exception. Doing this just passes the error message
back to the sub or function that called your SetActElement() routine:
Catch ex As Exception

Throw ex

If you want to totally ignore the error, you could drop the ex part.
Catch 'Ignore Error
End Try

I don't generally suggest this however, unless the error is one that is
harmless.

This is a more elegant way of handling ON ERROR GOTO if you recall that from
the VB6 days.

Hope this helps,

Robert
 
P

Phill W.

Hi I'm using the following code which is finally working.
Try
. . .

Catch ex As Exception

End Try

I understand most of what's going on but i've never met Catch, or Try
before

Do you remember "On Error Goto <label>" in VB "Proper"?
Try .. Catch is a /little/ bit like that, but gives you a lot more - /if/
you
make use of it.

Broadly, anything that throws an Exception within a Try block, jumps
immediately to the Catch block, just like "On Error Goto" used to.
However Try .. Catch goes /way/ beyond that, in that you you can have
/many/ Catch blocks, each handling different Exception(s) so you can deal
with different problems in different ways. Having finished the code in the
Catch block, VB continues (through the Finally block, if there is one), and
on to the next statement after the End Try.

BIG BUT ...

What you've coded here, though, is more like "On Error Resume Next"!
You say this is "finally working"? I would have to disagree, because you
don't actually know that for sure.
If an Exception does get thrown, sure you're catching it, but doing
/nothing/ with it and then just carrying on as if nothing was wrong.

NEVER have an empty Catch block.
At the very least, you need code in there to Log the Exception somewhere,
(or even just a comment to say why you don't /care/ about this Exception).

OK; your code isn't erroring any more, but by catching and suppressing
every possible Exception, that's not exactly a surprise.

Regards,
Phill W.
 
G

garyusenet

Thankyou both for your very informative replies. I have no VB
background at all and have only in the past few months decided to learn
programming - i'm enjoying every moment of it - but am learning c# -
however my first real world project needs an interface with an Act! 6
api. I couldn't get this working in c# but managed to get it working in
VB using the above code. The code was from a poster to usenet and so he
had used the try and catch and that's where my curiosity sprang from.

I now understand it, and I will add a little message box to warn of the
error if and when it's thrown.

Thank you both very much. I now understand this!
 
C

Cerebrus

Hi,

"Try" this :

Try
' to Do something that could be error-prone.
' Do something else
:
Catch
' Oops ! We have an error. Let's catch it, so that program doesn't
crash.
' Log or display the Exception message.
Finally
' Let's clean the mess up !
' Phew! All done, let's go home
:
End Try

Regards,

Cerebrus.
 

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

Top