Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

VB.net errohandling problem

Options
  • 18-02-2009 11:17am
    #1
    Closed Accounts Posts: 2,268 ✭✭✭


    I would like to force my program to continue when it his a particular error.

    I would like to handle a Null reference exception at a particular line in my code and only for a sepcific subroutine.

    Is this possible?

    Thanks

    MM


Comments

  • Registered Users Posts: 2,494 ✭✭✭kayos


    It would be better to check for the NULL reference before you run the code that would throw the exception. This is simply because from what you say it is a likely case to happen.

    Exceptions can be expensive so if you can avoid them do!


  • Closed Accounts Posts: 2,268 ✭✭✭mountainyman


    I have the below:
    Public Class Class1
        Private Sub chkBOX1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBOX1.CheckedChanged
    
            Try
                If chkBOX1.Checked = True Then
                    txtBOX1.Enabled = True
                End If
                If chkBOX1.Checked = False Then
                    txtBOX1.Enabled = False
                End If
            Catch
                MsgBox("You cannot set the BOX Value at this time.")
            End Try
        End Sub
    End Class
    


  • Registered Users Posts: 9,557 ✭✭✭DublinWriter


    A little simpler?...

    Public Class Class1
    Private Sub chkBOX1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBOX1.CheckedChanged

    Try
    txtBOX1.Enabled = chkBOX1.Checked
    Catch
    MsgBox("You cannot set the BOX Value at this time.")
    End Try

    End Sub
    End Class


  • Registered Users Posts: 2,494 ✭✭✭kayos


    Maybe its me but what the hell is throwing an exception in that? I can only guess it’s the txtbox is null as it has not been created yet and you dynamically add it.

    If that’s the case I would be doing
    If (txtBOX1 is nothing)
    {
    MsgBox("You cannot set the BOX Value at this time.")
    }
    Else
    {
    txtBOX1.Enabled = chkBOX1.Checked
    }
    

    This way you avoid causing an exception and the overhead that incurs, this to me would be the proper way of dealing with this if it is something that you need to allow happen. How ever some VB6 programmers like to use error handling to catch these things rather than check first and avoid the bloody errors.

    Would you jump off a ledge without checking to see it was a safe distance?


  • Closed Accounts Posts: 2,268 ✭✭✭mountainyman


    In fairness Kayos I am doing both. It looks exactly like I am instantiating the checkbox at runtime but I am not.

    However I will try adding the chec you reccomend.


    Thanks

    MM


  • Advertisement
  • Registered Users Posts: 2,494 ✭✭✭kayos


    In fairness Kayos I am doing both. It looks exactly like I am instantiating the checkbox at runtime but I am not.

    Sorry if it came across like I was giving out I'm not its just baffling how you can get an exception in that code if both the checkbox (it has to be our you could not click it :P) and txtbox are already there.

    I'll still stand by the view if you know it can happen and there is a business case for it happening then check and avoid rather than allow the exception to be thrown.


  • Closed Accounts Posts: 2,268 ✭✭✭mountainyman


    Thanks to dublin writer and kayos. I checked to see if the control that iis the text box was nothing and it was.

    Strange that a designer drawn conrol could be nothing.


Advertisement