On Error Goto: A Complete Guide

Updated November 5, 2026

Total time
15 min
prep 15 min · cook 0 min
Serves
1
one portion

Jump to recipe

On Error Goto: A Complete Guide

This isn’t a dish you eat, but a technique every home cook of code should have in their kitchen. “On Error Goto” is a classic error-handling directive in BASIC-derived programming languages (like VBA and Visual Basic) that tells your program exactly where to jump when something goes wrong. It’s the difference between a program that crashes with a cryptic message and one that gracefully handles a missing file or a bad input, making it an essential tool for writing robust, user-friendly macros and scripts.

Ingredients

  • 1 programming environment with VBA (e.g., Microsoft Excel, Access) or a BASIC dialect
  • 1 clear understanding of the task your code must perform
  • 1 error handler label (e.g., ErrorHandler:)
  • 1 Exit Sub or Exit Function statement (to prevent the handler from running after successful code)
  • 1 Resume or Resume Next statement (to tell the program where to go after handling the error)
  • A pinch of Err.Number and Err.Description (to identify and log the error)
  • Optional: A dash of On Error Resume Next (for ignoring non-critical errors, use sparingly)

Instructions

  1. Identify the risky section. Look for code that might fail: file operations, user input, division, or database queries. This is where you need error handling.
  2. Add the On Error Goto statement. At the beginning of the risky section, type On Error Goto ErrorHandler. This tells the program: “If anything goes wrong from here on, jump to the line labeled ErrorHandler.”
  3. Write the error handler label. At the end of your procedure (after a normal exit), add a line with just ErrorHandler: (or any name you choose, followed by a colon). This marks the spot where the program will jump when an error occurs.
  4. Add an Exit Sub (or Exit Function) before the label. This is crucial. Without it, the program will “fall through” into the error handler even when everything goes fine. Place Exit Sub right before your ErrorHandler: label.
  5. Handle the error. Inside the handler, decide what to do. You can show a friendly message using MsgBox "Error " & Err.Number & ": " & Err.Description, log the error to a file, or set a default value.
  6. Decide where to resume. After handling the error, you have three choices:
    • Resume – retries the line that caused the error (use only if the error is transient, like a missing file that you’ve now created).
    • Resume Next – continues with the line immediately after the one that errored (most common, safe choice).
    • Resume ExitHandler – jumps to a specific label, often used to run cleanup code before exiting.
  7. Test thoroughly. Run your code with and without errors to ensure the handler works and doesn’t interfere with normal operation. Try to trigger every error you anticipate.

Tips

  • Use On Error Resume Next sparingly. It’s a sledgehammer that ignores all errors, which can hide bugs. If you use it, always check Err.Number immediately after the risky line to see if an error occurred.
  • Don’t leave the handler empty. An empty handler is worse than no handler—it swallows errors and makes debugging a nightmare. At minimum, log the error or show a message.
  • Keep labels clear. Use descriptive names like FileError: or InputError: to make your code self-documenting. Avoid generic labels like ErrHandler: if you have multiple handlers.
  • Remember that On Error Goto is procedure-scoped. It only applies to the current Sub or Function. If you call another procedure, that procedure needs its own error handling.

Serving Suggestions

This technique is best served with a side of clear comments and a dash of logging. Use it in any procedure that touches the outside world—file I/O, user forms, or database connections. It pairs well with a debugging session and a cup of coffee.

Variations

  • Multiple error handlers: Use different labels for different sections of code, and Goto the appropriate one based on Err.Number.
  • Centralized error handler: Create a separate function that all your error handlers call, to log errors consistently across your project.
  • Inline error checking: Instead of On Error, check for errors manually (e.g., If Dir(file) = "" Then ...) for simple cases. This is often cleaner and more predictable.

Storage and Reheating

Your error-handling code will keep indefinitely in your source files. To “reheat,” simply review it when you revisit the project. There’s no expiration date, but be aware that changes to the environment (e.g., new file paths, different Excel versions) can cause new errors, so always test after any changes. If you copy code into a new project, make sure the error handler labels and Exit Sub statements are still correctly placed.

Frequently Asked Questions

Q: What’s the difference between On Error Goto and On Error Resume Next?
A: On Error Goto sends the program to a specific label where you can handle the error. On Error Resume Next tells the program to simply skip the errored line and continue with the next one. The former is for when you need to react to the error; the latter is for when you can safely ignore it (but you should still check Err.Number afterwards).

Q: Can I have multiple On Error Goto statements in one procedure?
A: Yes, you can change the active error handler as many times as you like. For example, you might set On Error Goto FileError before a file operation, then On Error Goto InputError before a user input section. Just make sure each label exists and is reachable.

Q: What happens if an error occurs inside the error handler itself?
A: That’s a classic bug. If an error occurs in your error handler, the program will crash because there’s no active error handler. To prevent this, you can add On Error Resume Next at the start of your handler, but it’s better to keep the handler simple and safe.

Q: Is On Error Goto the same in all programming languages?
A: No. It’s specific to BASIC-derived languages like VBA, VB6, and older versions of Visual Basic. Other languages have different mechanisms: Python uses try/except, C# uses try/catch/finally, and JavaScript uses try/catch. The concept is similar, but the syntax is different.

Q: How do I log the error to a file?
A: In VBA, you can use Open and Print # to write to a text file. For example, in your error handler: Open "C:\error.log" For Append As #1 then Print #1, Now & " - " & Err.Number & " - " & Err.Description then Close #1. This is invaluable for debugging.

Q: Is it okay to use On Error Goto 0?
A: Yes, On Error Goto 0 disables error handling in the current procedure. It’s useful when you want to revert to default (crash) behavior after a specific section. But use it carefully—it will cause the program to stop if an error occurs after that point.

Related

More recipes to cook

Safety notes: safe minimum internal temperatures from USDA FSIS; raw and undercooked fish guidance from the U.S. FDA. Food safety notes

Spotted an error? Tell us and we will correct it (corrections policy).