Error Messages (Visual Basic)

Error messages can occur while an application is running, either within the Visual Basic environment or as a stand-alone executable file. Some of these can also occur during design time or compile time.

For resources that you can use to assist in troubleshooting an error, see Additional Resources for Visual Basic Programmers.

Run Time Errors

Visual Basic run-time errors occur when an application attempts to perform an action that the system cannot execute.

The errors that Visual Basic throws are Exception objects. Visual Basic can generate custom errors of any data type, including Exception objects, by using the Throw statement. A program can display the error number and message of a caught exception to identify the error. If an error is not caught, the program ends.

Run-time errors can be trapped and examined by the code. By enclosing the code that produces the error in a Try block, any thrown error can be caught within a matching Catch block.

For information on trapping errors at run time and responding to them in your code, see Exception and Error Handling in Visual Basic.

Compile Time Errors

Visual Basic compiler errors occur when the compiler encounters problems in the code. The code causing the errors is marked with a wavy line underneath it in the Code Editor, and when your mouse pauses over the marked code, the error message appears. The messages also appear in the Task List.

The Smart Compile Auto Correction feature suggests corrections for some errors and allows you to apply the fix. When an error occurs, if there is a hollow rectangular symbol under the right side of the wavy underline, resting your mouse over the wavy underline or double-clicking the error message in the Task List turns the symbol into a smart tag panel. Clicking or resting your mouse pointer over the smart tag panel opens the Error Correction Options helper window, which displays a description of the error, as well as a set of suggestions for fixing the error. For more information, see How to: Correct Compiler Errors with Auto Correction (Visual Basic).

The Visual Basic compiler includes a set of warnings about code that may cause run-time errors. Developers can use that information to write cleaner, faster code with fewer bugs. For example, the compiler produces a warning when the user attempts to invoke a member of an unassigned object variable, return from a function without setting the return value, or execute a Try block with errors in the logic to catch exceptions. To learn more about warnings, including how to turn them on and off, see Configuring Warnings in Visual Basic.