exception issues

E

ECathell

I have an exception that is being caught and handled correctly in my VS IDE. However the compiled version throws an unhandled exception when it reaches that point...here is some code....

this is where the unhandled is being raised:

Try
........

Dim prodcol As New Mountaire.Database.Products.ProductsCollection(mLocation)
prodcol.Add(mNewProduct)
Catch ex As Mountaire.Database.Products.ProductExistsException
Me.Hide()
Me.DialogResult = DialogResult.Cancel
Throw New Mountaire.Database.Products.ProductExistsException
Catch ex As InvalidCastException
MessageBox.Show("You must fill in Pluno and Product Code.", "Invalid Input")
Me.DialogResult = DialogResult.Abort

Finally

End Try


however in the calling code this is what I have:
try
......
dlgResult = frm.ShowDialog

If dlgResult = DialogResult.OK Then
Me.fillProductInformation(mLocation)
index = CInt(frm.txtPluno.Text)
'debug.WriteLine(frm.NewProduct.PluNo)
Else
Throw New Mountaire.GenericException.ActionCanceledException
End If
Loop Until dlgResult <> DialogResult.Abort

Catch ex As Mountaire.GenericException.ActionCanceledException
index = 0
Catch ex As Products.ProductExistsException
MessageBox.Show(ex.Message, "Product Already Exists")
index = CInt(frm.txtPluno.Text)
Catch ex As Exception
MessageBox.Show(ex.ToString, "Exception Thrown")
Finally
Me.mProductCM.Position = mProductCollection.IndexOf(index)

End Try


Any Ideas?
 
M

Marina

A couple of things

1) What is the unhandled exception? What does it say? Perhaps it is the CInt call in the catch block throwing the exception
2) It looks like you are using exceptions as a signal of a business error. This is generall not recommended. Throwing errors is meant for critical application problems. For logical errors, you should be returning a code to indicate this, and then dealing with the problem accordingly. Additionally, throwing exceptions to indicate logic errors all over the place is going to hurt performance.
I have an exception that is being caught and handled correctly in my VS IDE. However the compiled version throws an unhandled exception when it reaches that point...here is some code....

this is where the unhandled is being raised:

Try
.......

Dim prodcol As New Mountaire.Database.Products.ProductsCollection(mLocation)
prodcol.Add(mNewProduct)
Catch ex As Mountaire.Database.Products.ProductExistsException
Me.Hide()
Me.DialogResult = DialogResult.Cancel
Throw New Mountaire.Database.Products.ProductExistsException
Catch ex As InvalidCastException
MessageBox.Show("You must fill in Pluno and Product Code.", "Invalid Input")
Me.DialogResult = DialogResult.Abort

Finally

End Try


however in the calling code this is what I have:
try
.....
dlgResult = frm.ShowDialog

If dlgResult = DialogResult.OK Then
Me.fillProductInformation(mLocation)
index = CInt(frm.txtPluno.Text)
'debug.WriteLine(frm.NewProduct.PluNo)
Else
Throw New Mountaire.GenericException.ActionCanceledException
End If
Loop Until dlgResult <> DialogResult.Abort

Catch ex As Mountaire.GenericException.ActionCanceledException
index = 0
Catch ex As Products.ProductExistsException
MessageBox.Show(ex.Message, "Product Already Exists")
index = CInt(frm.txtPluno.Text)
Catch ex As Exception
MessageBox.Show(ex.ToString, "Exception Thrown")
Finally
Me.mProductCM.Position = mProductCollection.IndexOf(index)

End Try


Any Ideas?
 
E

ECathell

thanks for the tips...I attached to the process and its breaking on the exception that would return control to the calling form.

the unhandled exception is the message of my ProductExistsException...

Mountaire.Database.Products.ProductExistsException: This Product Already Exists
at Mountaire.Database.Products.ProductsCollectionBase.Add(Product obj)
at frmAddNewProduct.btnOK_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

I will rethink my exception strategies...I may have misinterpreted some article I read before I started doing this. Maybe I should be using events instead of exceptions...anyway the article was about different strategies to terminate loops and handle error conditions in applications...


--
--Eric Cathell, MCSA
A couple of things

1) What is the unhandled exception? What does it say? Perhaps it is the CInt call in the catch block throwing the exception
2) It looks like you are using exceptions as a signal of a business error. This is generall not recommended. Throwing errors is meant for critical application problems. For logical errors, you should be returning a code to indicate this, and then dealing with the problem accordingly. Additionally, throwing exceptions to indicate logic errors all over the place is going to hurt performance.
I have an exception that is being caught and handled correctly in my VS IDE. However the compiled version throws an unhandled exception when it reaches that point...here is some code....

this is where the unhandled is being raised:

Try
.......

Dim prodcol As New Mountaire.Database.Products.ProductsCollection(mLocation)
prodcol.Add(mNewProduct)
Catch ex As Mountaire.Database.Products.ProductExistsException
Me.Hide()
Me.DialogResult = DialogResult.Cancel
Throw New Mountaire.Database.Products.ProductExistsException
Catch ex As InvalidCastException
MessageBox.Show("You must fill in Pluno and Product Code.", "Invalid Input")
Me.DialogResult = DialogResult.Abort

Finally

End Try


however in the calling code this is what I have:
try
.....
dlgResult = frm.ShowDialog

If dlgResult = DialogResult.OK Then
Me.fillProductInformation(mLocation)
index = CInt(frm.txtPluno.Text)
'debug.WriteLine(frm.NewProduct.PluNo)
Else
Throw New Mountaire.GenericException.ActionCanceledException
End If
Loop Until dlgResult <> DialogResult.Abort

Catch ex As Mountaire.GenericException.ActionCanceledException
index = 0
Catch ex As Products.ProductExistsException
MessageBox.Show(ex.Message, "Product Already Exists")
index = CInt(frm.txtPluno.Text)
Catch ex As Exception
MessageBox.Show(ex.ToString, "Exception Thrown")
Finally
Me.mProductCM.Position = mProductCollection.IndexOf(index)

End Try


Any Ideas?
 
M

Marina

What I would do in this case, is first see if the product is in the collection. If it is, I would return the appropriate code to the calling form, so it could handle this case appropriately. If the product is not, then it is safe to add it.

I am not sure if events are appropriate here, at least from what you have described.
thanks for the tips...I attached to the process and its breaking on the exception that would return control to the calling form.

the unhandled exception is the message of my ProductExistsException...

Mountaire.Database.Products.ProductExistsException: This Product Already Exists
at Mountaire.Database.Products.ProductsCollectionBase.Add(Product obj)
at frmAddNewProduct.btnOK_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

I will rethink my exception strategies...I may have misinterpreted some article I read before I started doing this. Maybe I should be using events instead of exceptions...anyway the article was about different strategies to terminate loops and handle error conditions in applications...


--
--Eric Cathell, MCSA
A couple of things

1) What is the unhandled exception? What does it say? Perhaps it is the CInt call in the catch block throwing the exception
2) It looks like you are using exceptions as a signal of a business error. This is generall not recommended. Throwing errors is meant for critical application problems. For logical errors, you should be returning a code to indicate this, and then dealing with the problem accordingly. Additionally, throwing exceptions to indicate logic errors all over the place is going to hurt performance.
I have an exception that is being caught and handled correctly in my VS IDE. However the compiled version throws an unhandled exception when it reaches that point...here is some code....

this is where the unhandled is being raised:

Try
.......

Dim prodcol As New Mountaire.Database.Products.ProductsCollection(mLocation)
prodcol.Add(mNewProduct)
Catch ex As Mountaire.Database.Products.ProductExistsException
Me.Hide()
Me.DialogResult = DialogResult.Cancel
Throw New Mountaire.Database.Products.ProductExistsException
Catch ex As InvalidCastException
MessageBox.Show("You must fill in Pluno and Product Code.", "Invalid Input")
Me.DialogResult = DialogResult.Abort

Finally

End Try


however in the calling code this is what I have:
try
.....
dlgResult = frm.ShowDialog

If dlgResult = DialogResult.OK Then
Me.fillProductInformation(mLocation)
index = CInt(frm.txtPluno.Text)
'debug.WriteLine(frm.NewProduct.PluNo)
Else
Throw New Mountaire.GenericException.ActionCanceledException
End If
Loop Until dlgResult <> DialogResult.Abort

Catch ex As Mountaire.GenericException.ActionCanceledException
index = 0
Catch ex As Products.ProductExistsException
MessageBox.Show(ex.Message, "Product Already Exists")
index = CInt(frm.txtPluno.Text)
Catch ex As Exception
MessageBox.Show(ex.ToString, "Exception Thrown")
Finally
Me.mProductCM.Position = mProductCollection.IndexOf(index)

End Try


Any Ideas?
 

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