|
-
Aug 10th, 2007, 07:03 AM
#1
Thread Starter
Hyperactive Member
[2005] Try....Catch Not Working Properly
Hello,
I am using a try....catch statement in my code that doesn't seem to be working properly. I may be doing something a bit different to the standard, so I'll explain the best I can.
I have a 3 tier application, and in my class I have the following code :
Code:
Try
AdaptSql = New Data.DetailsAdapter(SQL_ConnectionString)
m_TDS = New DetailsTDS
AdaptSql.Fill(m_TDS, m_EERef)
If m_TDS.Tables(0).Rows.Count = 0 Then
m_Row = m_TDS.tbl_Details.NewRow
m_Row.ID = m_ID
m_TDS.tbl_Details.Addtbl_DetailsRow(m_Row)
m_Row.EndEdit()
AdaptSql.Update(m_TDS)
Else
MsgBox("This Reference already exists!", MsgBoxStyle.Critical, "Reference Already Exists")
End If
m_Row = Nothing
m_TDS.Dispose()
AdaptSql = Nothing
Catch ex As Exception
MsgBox(ex.Message)
End Try
All this does is uipdate the table if the reference doesn't already exist, and if it does, it brings back the message advising that the update failed.
This works fine.
However in my code on the form I also have :
Code:
Try
ID.Update_New(txtID.Text)
MessageBox.Show("Your New ID has been logged.", "ID Added", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MsgBox(ex.Message)
End Try
Now I would expect that if the code in the class fails (because the reference already exists), then the messagebox here shouldn't show.
However it does - It seems as though regardless of what happens in the class (whether the ID is added or not) both lines in the form run.
What am I doing wrong please ?
-
Aug 10th, 2007, 07:25 AM
#2
Lively Member
Re: [2005] Try....Catch Not Working Properly
Something like?
Code:
Try
AdaptSql = New Data.DetailsAdapter(SQL_ConnectionString)
m_TDS = New DetailsTDS
AdaptSql.Fill(m_TDS, m_EERef)
If m_TDS.Tables(0).Rows.Count = 0 Then
m_Row = m_TDS.tbl_Details.NewRow
m_Row.ID = m_ID
m_TDS.tbl_Details.Addtbl_DetailsRow(m_Row)
m_Row.EndEdit()
AdaptSql.Update(m_TDS)
Try
ID.Update_New(txtID.Text)
MessageBox.Show("Your New ID has been logged.", "ID Added", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
MsgBox("This Reference already exists!", MsgBoxStyle.Critical, "Reference Already Exists")
End If
m_Row = Nothing
m_TDS.Dispose()
AdaptSql = Nothing
Catch ex As Exception
MsgBox(ex.Message)
End Try
-
Aug 10th, 2007, 07:34 AM
#3
Re: [2005] Try....Catch Not Working Properly
Considering that you have two messageboxes on the form, your question is not quite clear. However, if an exception is caught in the function, there will be no further exception to be caught by the form unless you re-throw the exception in the function.
Since you catch all exceptions in the function, there is no value in even trying to catch them in the form. There won't be any, and a simple return value will work better and more efficiently.
My usual boring signature: Nothing
 
-
Aug 10th, 2007, 08:05 AM
#4
Thread Starter
Hyperactive Member
Re: [2005] Try....Catch Not Working Properly
Last edited by Jonny1409; Aug 10th, 2007 at 10:14 AM.
-
Aug 10th, 2007, 10:13 AM
#5
Thread Starter
Hyperactive Member
Re: [2005] Try....Catch Not Working Properly
After doing some more digging I think I have to use the Throw command to throw the error back to the procedure that called it, but to be honest I don't really know much about this.
Firstly am I correct ?
If so, I have put the following code in my class but it doesn't quite work properly.
Whilst it does pass me back the error, it also gives me a load of extra text I don't want - I just want a nice pretty message box.
What do I need to do please ?
Code:
Try
AdaptSql = New Data.DetailsAdapter(SQL_ConnectionString)
m_TDS = New DetailsTDS
AdaptSql.Fill(m_TDS, m_EERef)
If m_TDS.Tables(0).Rows.Count = 0 Then
m_Row = m_TDS.tbl_Details.NewRow
m_Row.ID = m_ID
m_TDS.tbl_Details.Addtbl_DetailsRow(m_Row)
m_Row.EndEdit()
AdaptSql.Update(m_TDS)
Else
Throw New ArgumentException("An Employee with this Reference already exists!")
End If
m_Row = Nothing
m_TDS.Dispose()
AdaptSql = Nothing
Catch ex As Exception
Throw
End Try
-
Aug 10th, 2007, 08:34 PM
#6
Re: [2005] Try....Catch Not Working Properly
You're on the right track, but there are a couple of points that aren't quite clear.
1) There is NO cost to using a Try...Catch as long as you never have an exception, but if you DO have an exception, the cost gets pretty high. Therefore, if you can avoid using exceptions, then do so. It appears that you are throwing an exception that you immediately catch, then re-throw. That's not a good idea if you can possibly avoid it. Since you are already using an If statement to confirm that there is a problem, then don't throw an exception there. You know the problem exists, deal with it then and there.
2) Throwing an exception out of a catch block is actually quite typical for what you are trying to do, and showing ex.Message should be sufficient. However, you just have a Throw statement in the Catch block, without any message. I'm pretty sure you have the right syntax in the first highlighted block, but not so useful in the second.
3) Having said that, if there is ANY way you can indicate that the function didn't work without re-throwing the exception (such as using a return value), then that would be a superior means of doing this.
My usual boring signature: Nothing
 
-
Aug 13th, 2007, 03:13 AM
#7
Thread Starter
Hyperactive Member
Re: [2005] Try....Catch Not Working Properly
Hi shaggy,
Thanks for your advice, but I'll be totally honest and say I don't fully understand. I'm pretty new to this, and in particular throwing exceptions - Its the first time I've used them.
1) How can I deal with the problem then and there - when I was doing this before it wasn't getting passed back to my form, and therefore the code after this line was still getting executed despite this line failing.
2) I've no idea what my code should be in the first or the 2nd, I kind of just stumbled across this.
3) I don't know how I would do this using a return value of some kind.
I'm open to suggestions on how I can accomplish this as I have a feeling I'm way out of line with this.
-
Aug 13th, 2007, 06:57 AM
#8
Re: [2005] Try....Catch Not Working Properly
All that code is in a sub or function, right? If it is in a sub, change it to a function with a return value. Either return a boolean, such as True for success or false for failure, or return an integer where different numbers mean different things. Perhaps 0 would mean success, while different kinds of failure could be indicated by different numbers.
Then the key would be to Return the proper value from the exception handler. That would work faster than throwing an exception, though there are times when throwing an exception is done.
I think the reason that nothing was getting passed back was that when you catch an exception, then it gets cleared, so the program feels that all is well, even though all is not well. The program doesn't know that you were unable to solve anything regarding the exception, so it will keep on going. You need to explicitly Return if you can't keep on going.
My usual boring signature: Nothing
 
-
Aug 13th, 2007, 09:33 AM
#9
Thread Starter
Hyperactive Member
Re: [2005] Try....Catch Not Working Properly
Thanks shaggy, I'll give that a go.
Cheers.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|