Actually, I'd be willing to bet that it IS occurring within a Try/Catch but the fault of the programmer is that he is Catching a generic
Exception. If he were catching soemthing more specific then this type of exception wouldnt be being absorbed by his blanket Catch and the IDE would break at the point the exception occurred because it is unhandled.
Though it is possible for a programmer to throw this exception directly, "Object reference not set to an instance" is a standard error message usually thrown by the CLR without direct instruction from the programmer to do so. It is often the result of programmer error; by failing to forsee a combination of parameters or variables that would lead to a situation where a vital object reference remains uninstantiated, the programmer generates cause for this error to appear. On its most basic level, the error will be thrown by this code:
Sub Main()
Dim o as Object = Nothing
MessageBox.Show(o.ToString())
End Sub
The (pre)compiler will not report that o is not pointing to an instance. It would only report in this siutation:
Sub Main()
Dim o as Object
MessageBox.Show(o.ToString())
End Sub
Note the missing
= Nothing
So, it is likely that this error isnt being thrown by the programmer's directive, but implicitly by the CLR because the programmer is attempting to use an object that has been initialized or reset to Nothing.
Following the procedure I described for having the CLR break as soon as an exception is thrown rather than unhandled should work for this instance.
We can learn two things from this:
One, it is foolish to write Catch(Exception) if your code block only knows how to handle a specific exception - you should catch that instead. If you must write a generic catch-all then the error message should include the StackTrace, vis:
Try
'dodgy operation
Catch(se As SpecificException)
'deal with specific
Catch(ex As Exception)
MessageBox.Show("Encountered an error I cant handle: " & ex.StackTrace)
End Try
If the stacktrace were made available to the user, they would see the exact line number of the method that raised the exception (if in debug mode at least. In release mode, line number symbols would be absent but it would still be handy to have this error message). It is important for developers to remember that a stack trace may be tens or hundreds of calls deep, and their try/catch scenarios may be handling errors generated in different modules, maybe even in different projects.
The other is that vital objects should be tested to see that <
objectReference> IsNot Nothing before they are used. If they are found to be nothing, then a more specific error can be raised, rather than having to work out which of the hundreds of potential candidates is wasting debugging time