Try ... Catch Loop

J

Jerry

Hi All,

I want to fill some textbox with properties from Active Directory. The fact
is if the property is unavailable the program will crash. You can prevent
this by using a try and catch. So far, so good. Now I want to fill 'allot' of
text boxes. Doing a try and all the statements ending with a catch gives me
the problem that the rest of the statement will not be checked. So how can I
check every statement in one try catch 'loop' without using a separate try
catch for each?
For example:
Try
txtVoornaam.Text = usr.FirstName
txtInitialen.Text = usr.initials
txtAchternaam.Text = usr.LastName
Catch ex As Exception
End Try
Will stop executing if "txtVoornaam.Text = usr.FirstName" will give an
error, while it should check out the next two statements. I can do this:
Try
txtVoornaam.Text = usr.FirstName
Catch ex As Exception
End Try
Try
txtInitialen.Text = usr.initials
Catch ex As Exception
End Try
Etc.

But it's allot of code given that I have 20 of those.

Anyone can help me it will be appreciated?
 
J

Jerry

Littel add-on:

Probably I can do a “on error resume nextâ€, but this give me the challenge
that I have some statements (within the same method) that are required an
alternative text if there is an error.

Example:
Try
txtEMail.Text = usr.EmailAddress
Catch ex As Exception
txtEMail.Text = "Geen Mailadres"
End Try
 
M

Mr. Arnold

Jerry said:
Hi All,

I want to fill some textbox with properties from Active Directory. The
fact
is if the property is unavailable the program will crash. You can prevent
this by using a try and catch. So far, so good. Now I want to fill 'allot'
of
text boxes. Doing a try and all the statements ending with a catch gives
me
the problem that the rest of the statement will not be checked. So how can
I
check every statement in one try catch 'loop' without using a separate try
catch for each?
For example:
Try
txtVoornaam.Text = usr.FirstName
txtInitialen.Text = usr.initials
txtAchternaam.Text = usr.LastName
Catch ex As Exception
End Try
Will stop executing if "txtVoornaam.Text = usr.FirstName" will give an
error, while it should check out the next two statements. I can do this:
Try
txtVoornaam.Text = usr.FirstName
Catch ex As Exception
End Try
Try
txtInitialen.Text = usr.initials
Catch ex As Exception
End Try
Etc.

But it's allot of code given that I have 20 of those.

Anyone can help me it will be appreciated?

So why do you have this in some kind of try/catch?

Cannot usr.whatever be checked for it being an object before you do
anything? If it exist (not nothing or not null) you do something with it,
and you don't if it (is nothing or is null) you bypass it.

Why do you have to have some kind of try/catch?
 
A

Alan Gillott

Create a little Function that passes both parameters and do the try catch in
there. That way you just issue the Subroutine calls in the main logic.
 
J

Jerry

Tanks for the reply,

For both I would like try both, but to be hounest I don't know how. Could
you please help on the way with a little example or something. I try to
understand it all, but not be a real 'programmer' doen't help :)
 
M

Mr. Arnold

Jerry said:
Tanks for the reply,

For both I would like try both, but to be hounest I don't know how. Could
you please help on the way with a little example or something. I try to
understand it all, but not be a real 'programmer' doen't help :)

Maybe you need to do the test. Eveything in .Net is an object and either
it's an object or it's not an object, it actually exist.

If Not usr.FirstName Is Nothing Then
txtVoornaam.Text = usr.FirstName

EndIf

Do a little test and play with it.

Dim TestA as string = Nothing

If usr.FirstName Is Nothing Then
Messagebox.Show("object is nothing")

EndIf

TestA = "Test"

If Not usr.FirstName Is Nothing Then

Messagebox.Show("object is something and object = " &TestA)
EndIf
 
C

Cor Ligthert[MVP]

Jerry-

Private Sub ZetTextInControls(byval text as string, byval dezeControl as
Control)
try
dezeControl.text = text
catch
MessageBox.Show("What je ook wilt, kan ook weer een methode zijn")
end catch
end sub

When you fill the controls (whatever control because every control has a
property text as that is in the base Control class)

ZetTextInControls("Jerry", TextBox1)

-Cor
 
J

Jerry

Thanks alot! I just love MS newsgroups. Serious people really trying to help.
I will try the suggestions.
 

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