473,770 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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("a ctole.database" )
objApp.OpenEx(" ")
If objApp.IsOpen = False Then
MessageBox.Show ("Failed to connect to ACT")
Application.Exi t()
End If
ActElement = objApp.contact

' get the database path
' MsgBox(objApp.G etDataBasePath)

ActElement.Add( )
ActElement.Data (26, "A SuperStar")
ActElement.Upda te()

Catch ex As Exception

End Try
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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
4 4665
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********@myw ay.com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.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("a ctole.database" )
objApp.OpenEx(" ")
If objApp.IsOpen = False Then
MessageBox.Show ("Failed to connect to ACT")
Application.Exi t()
End If
ActElement = objApp.contact

' get the database path
' MsgBox(objApp.G etDataBasePath)

ActElement.Add( )
ActElement.Data (26, "A SuperStar")
ActElement.Upda te()

Catch ex As Exception

End Try
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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

<ga********@myw ay.com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
2362
by: Steven T. Hatton | last post by:
If I understand correctly, I have no assurance that I can determine the type of a simple class instance thrown as an exception unless I explicitly catch it by name. (non-derived classes having no virtual funcitons have no rtti) That is, there is no way to do something like: try{ funct_from_3rd_party(); } catch(...){ std:err << extract_name() << std::endl; }
9
343
by: Steven Blair | last post by:
Hi, I need to catch exceotions on File.Delete() After checking the help, I have noticed that thgere are serevral Exceptions that can be thrown. My question is, should I catch all thes Exceptions, or if I simply do the following: try
13
5058
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
24
2176
by: Dave | last post by:
Maybe I'm missing something here, but I can't see the purpose of the 'finally' keyword. What is the difference between: try { doSomething() } catch { handleError(); }
4
2040
by: Rob Richardson | last post by:
Greetings! I am working on an application that targets a Pocket PC running Windows CE and SQL Server CE. Almost all functions in the application use a Try block with a Catch block that looks like this: Try TryToDoIt() Catch e as Exception LogTheError(e)
5
287
by: tni | last post by:
int x = 0; int y = 0; int z = 0; try { z = x / y; } catch (Exception) // THIS IS NOT A (DECLARATION)!!!! { }
11
1676
by: Howard Kaikow | last post by:
I'm using the code below, but an error is not getting trapped. Where can such errors occur? In another process? Try SomeCode Catch ex As Exception Dim strMsg() As String = Split(ex.ToString, vbCrLf) With lstStuff For i = 0 To UBound(strMsg)
12
1793
by: EvilOldGit | last post by:
In Stroustrup he talks of a MathErr class that you can use to catch floating point exceptions. It doesn't exist, at least not in Visual C++ I can catch FP exceptions using catch(...) but am stumped in finding out what the class I'm catching is. I would like to know how to catch them in a more elegant way than catch(...) and then poling around in registers to guess the problem. TIA
28
3810
by: gnuist006 | last post by:
I have some code like this: (if (test) (exit) (do something)) or (if (test)
1
10008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8891
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2822
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.