Sunday, October 14, 2012

Object reference not set to an instance of an object (part 3)

Object reference not set to an instance of an object (part 3)

StackTrace Property


The use of the function catch in the function try is very important. It is so important because it allows the programmer to build programs faster. From my humble opinion, I've always found that too many programmers neglect error handling in their conceptions of computer code. The result is that development times are longer and of lower quality. Employers who submit projects must impose basic techniques for handling errors. Because time is money.

Today, I took the opportunity to change the function catch easily. This change will allow the programmer to better identify and quickly correct errors or stops programs. We will use the StackTrace property. Watch the following code:

''' <summary>
''' crash test and how to improve your coding
''' </summary>
''' <remarks></remarks>
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub
    ''' <summary>
    ''' pressing button1 will crash he program
    ''' this crash is the typical crashing type for all beginners
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim aString() As String 'declare and array only


        Try
            'a crash will occur and the code won't be able to handle it
            MsgBox(aString(0)) ' you do something with the array and and array is nothing

        Catch ex As Exception
            MsgBox(ex.StackTrace) 'add basic error message here
        End Try

       
    End Sub
End Class


Others will write a custom phrase in the Msgbox. For example: "Error in function displays the table, please contact your Mr. XYZ."
In the two cases cited as an example, you will find cumbersome to identify where the error occurred. This is why the StackTrace property displays the function and the line of code where the error occurred.

When generating the error, the error message becomes much more explicit. (See image)
We immediately see the file, the function and the line of code where the error occurred.
So, you could line the line and directly infer the causes of the error message. Ends managements error complicated and customized for each of your many functions.
Eventually the error messages that mean nothing:

About

I invite you to visit my blog for more articles and leave a comment.
Check Technologies represents more than 10 years .... In computing technology and computer aided design.





No comments:

Post a Comment