Home > Articles > Programming > Visual Basic

  • Print
  • + Share This
This chapter is from the book

Handling Runtime Errors

You may have taken all the bugs out of your code, but there's another kind of problem that is often impossible to head off—runtime errors, called exceptions in Visual Basic .NET. A runtime error occurs when your program is asked to do something it can't do—divide a number by zero, for example, or open a file that doesn't exist. Tomorrow we'll start working with Windows forms programming and creating programs ready for public release, so it's a good idea to know how to handle runtime errors; otherwise, they'll crash your program.

Visual Basic .NET has good support for handling runtime errors. In fact, there are two ways of handling exceptions in Visual Basic .NET: unstructured (the VB6 way) and structured (the Visual Basic .NET way). We'll see them both today.

Unstructured exception handling centers on the On Error GoTo statement, whereas structured exception handling centers on the Try/Catch statement. "Unstructured" exception handling is called that because you can handle it anywhere in your code with the On Error GoTo statement, whereas "structured" exception handling with the Try/Catch statement restricts exception handling to specific code blocks (as an If statement does). We'll see unstructured exception handling first.

  • + Share This
  • 🔖 Save To Your Account