Search code examples
vb.netvisual-studio-2008error-handlingtry-catchonerror

In Try - Catch how the line of error can be found


We are using visual studio. In case of try & catch we cant locate exactly the line of code for which error is thrown. Where as if we use resume, exact line is shown & we can make corrections there & test. Some times in testing environment reproducing error may not be possible in many cases. When error is thrown we have to atleast locate the error there itself. Further if procedure is a big one like having more than 400 lines then locating error without line of error is a big headache. When try catch is considered superior to on error statement, why is this feature not available? While we were using vb6 we could just type resume & check the error line. In vb.net we are searching for that feature.


Solution

  • In VB6 the Err object is rather primitive, and gives you basic information about an error - a number and a message. "Error" state was easy to dismiss (On Error Resume Next) and proper error handling would obscure any method's intent.

    .NET exceptions are more sophisticated. They're special objects that can actually contain every single bit of available information that's needed to locate an error - including the specific line of code that caused it.

    That's because exceptions bubble up until they're caught (in a catch) block, and if they're not, they are unhandled and cause the program to stop. The exception will contain not only the type of error with a description and the very line of code that has thrown it, but also every single call that led to it - doing that in VB6 would require tremendous amount of meticulous "stack trace" building that could easily start lying, and wouldn't give you exact line numbers. .NET stack traces never lie.

    To view the stack trace, you can place a breakpoint in any catch block and look at the exception's properties.

    You can't resume like you would in VB6, because there could be dozens of method calls between the line that threw the exception and the line that catches it. But, like in VB6, you can move the yellow "current instruction" marker to another line and resume execution and re-run the try block line-by-line (F10) to see what's going wrong.