By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,475 Members | 954 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,475 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Mar 23 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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)
End Try
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
End Try
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

<ga********@myway.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com... 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.



----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Mar 23 '06 #2

P: n/a

<ga********@myway.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
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.
Mar 24 '06 #3

P: n/a
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!

Mar 24 '06 #4

P: n/a
Hi,
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


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

Mar 24 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.