Exit - VBScript


Exit command terminates the execution of the current script or function in VBScript. It is commonly used for error handling, conditional branching, and exiting loops.


Exit [Function | Sub]
  • Function: Exits the current function and returns control to the calling function.
  • Sub: Exits the current subroutine and returns control to the calling code.




Simple Example:

If err.number <> 0 Then Exit Function

Complex Example:

For i = 1 To 10
    If i = 5 Then Exit For

Common Issues

  • Exiting Multiple Nested Functions/Subroutines: Using the Exit command within nested functions or subroutines can lead to unexpected behavior. Consider using more structured error handling techniques like On Error Resume Next instead.
  • Exiting Loops Incorrectly: Exiting loops using Exit without proper handling can result in loss of control and potential script errors. Always ensure proper loop termination logic.


  • Error Handling: Exit can be used in conjunction with the On Error statement for error trapping and graceful exit from scripts.
  • Conditional Branching: Exit can be used to implement conditional branching and early termination based on specific conditions.
  • Multiple Exit Points: Exit can be used to create multiple exit points within scripts, allowing for more flexible control flow.
  • Err.Number: Returns the error number for the current error.
  • On Error Resume Next: Continues execution after an error occurs.
  • Return: Returns a value from a function or subroutine.