[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

Try- catch

I have a block as this


                Try
                    'Comprobar valores ""
                    If txtkilosLA.Text <> "" Then
                        dr.KgsLA = txtkilosLA.Text
                    Else
                        dr.SetKgsLANull()
                    End If
                    If txtkilosProv.Text <> "" Then
                        dr.KgsPro = txtkilosProv.Text
                    Else
                        dr.SetKgsProNull()
                    End If
...........

                 
                Catch ex As Exception
                    MessageBox.Show("Error antes de C14 " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End Try

How I can know what is the exact field with error, wittout a try-catch for each field?

thanks.
0
miguelmam
Asked:
miguelmam
1 Solution
 
rodmjayCommented:
Use the Throw New Exception to trigger specific exceptions, also you could define an inherited class like this for example and when something would trigger a specific exception in the try catch block, instantiate this class w/ a statement like this

Throw new payrollexception(Message, FieldInErrorNAme)

With the class looking like this:

Public Class PayrollException
      Inherits System.ApplicationException

      Private mstrFieldInError As String

      Sub New(ByVal strMessage As String, ByVal strFieldInError As String)
            ' Set the message for the new exception.

            MyBase.New(strMessage)
            mstrFieldInError = strFieldInError
      End Sub

      Public ReadOnly Property FieldInError() As String
            Get
                  Return mstrFieldInError
            End Get
      End Property
End Class
0
 
miguelmamAuthor Commented:
But, How can I define with is the FieldInErrorNAme?

Please add your code to the mine to made funcionality.

thanks,

0
 
p_sieCommented:
Try something like

        Dim i As Long

        Try
                    i=0
                    'Comprobar valores ""
                    If txtkilosLA.Text <> "" Then
                        dr.KgsLA = txtkilosLA.Text
                    Else
                        dr.SetKgsLANull()
                    End If
                    i = 1
                    If txtkilosProv.Text <> "" Then
                        dr.KgsPro = txtkilosProv.Text
                    Else
                        dr.SetKgsProNull()
                    End If
                    i=2
...........

                 
                Catch ex As Exception
                    SELECT CASE i
                          CASE 0
                                  MsgBox "txtkilosLA"
                          CASE 1
                                  MsgBox "txtkilosProv"
                          CASE ELSE
                                  MsgBox "Else
                   END SELECT
                    MessageBox.Show("Error antes de C14 " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End Try


Hope you understand what I mean

P_sie
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now