Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8053311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:48:12+00:00 2026-06-05T07:48:12+00:00

Intro There’s a lot of advice out there for dealing with return codes in

  • 0

Intro

There’s a lot of advice out there for dealing with return codes in batch files (using the ERROLEVEL mechanism), e.g.

  • Get error code from within a batch file
  • ERRORLEVEL inside IF

Some of the advice is to do if errorlevel 1 goto somethingbad, while others recommend using the
%ERRORLEVEL% variable and using ==, EQU, LSS, etc. There seem to be issues within IF statements and such, so then delayedexpansion is encouraged, but it seems to come with quirks of its own.

Question

What is a foolproof (i.e. robust, so it will work on nearly any system with nearly any return code) way to know if a bad (nonzero) code has been returned?

My attempt

For basic usage, the following seems to work ok to catch any nonzero return code:

if not errorlevel 0 (
    echo error level was nonzero
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:48:13+00:00Added an answer on June 5, 2026 at 7:48 am

    Sorry, your attempt is not even close. if not errorlevel 0 is only true if errorlevel is negative.

    If you know that errorlevel will never be negative, then

    if errorlevel 1 (echo error level is greater than 0)
    

    If you must allow for negative errorlevel, and are not within a parenthesized block of code, then

    set "errorlevel=1"
    set "errorlevel="
    if %errorlevel% neq 0 (echo error level is non-zero)
    

    Note – I edited my answer to explicitly clear any user defined errorlevel value after reading Joey’s comment to the linked answer in the question. A user defined errorlevel can mask the dynamic value that we are trying to access. But this only works if your script has a .bat extension. Scripts with .cmd extension will set your ERRORLEVEL to 0 if you set or clear a variable! To make matters worse, XP will set ERRORLEVEL to 1 if you attempt to undefine a variable that does not exist. That is why I first explicitly define an ERRORLEVEL variable before I attempt to clear it!

    If you are within a parenthesized block of code then you must use delayed expansion to get the current value

    setlocal enableDelayedExpansion
    (
      SomeCommandThatMightGenerateAnError
      set "errorlevel=1"
      set "errorlevel="
      if !errorlevel! neq 0 (echo error level is non-zero)
    )
    

    But sometimes you don’t want delayed expansion enabled. All is not lost if you want to check the error level immediately after executing a command.

    (
      SomeCommandThatMightGenerateAnError && (echo Success, no error) || (echo There was an error)
    )
    

    If you absolutely must check the dynamic ERRORLEVEL value without using delayed expansion within a parenthesized block, then the following works. But it has the error handling code in two places.

    (
      SomeCommandThatMightGenerateAnError
      if errorlevel 1 (echo errorlevel is non-zero) else if not errorlevel 0 (echo errorlevel is non-zero)
    )
    

    Here, at long last, is the “ultimate” test for non-zero errrolevel that should work under any circumstances 🙂

    (
      SomeCommandThatMightGenerateAnError
      set foundErr=1
      if errorlevel 0 if not errorlevel 1 set "foundErr="
      if defined foundErr echo errorlevel is non-zero
    )
    

    It can even be converted into a macro for ease of use:

    set "ifErr=set foundErr=1&(if errorlevel 0 if not errorlevel 1 set foundErr=)&if defined foundErr"
    (
      SomeCommandThatMightGenerateAnError
      %ifErr% echo errorlevel is non-zero
    )
    

    The macro supports parentheses and ELSE just fine:

    %ifErr% (
      echo errorlevel is non-zero
    ) else (
      echo errorlevel is zero
    )
    

    One last issue:

    Redirection of input and/or output can fail for any number of reasons. But redirection errors do not set the errorlevel unless the || operator is used. See File redirection in Windows and %errorlevel% for more information. So one can argue that there does not exist a fool-proof way to check for errors via errorlevel. The most reliable method (but still not infallible) is the || operator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there's a lot of other questions out there that deal with this
Using PHP, is there a nice way to get the (parsed) introduction only from
I'm monitoring a folder using FileSystemWatcher. If I download a file into there, how
Intro In the application I 'm currently working on, there are two kinds of
I have two sound files: Sound A is an 18 second intro designed to
Intro Hi there, I have a table made with lists <ul> that looks like
Is there any way to create animated app intro in cocos2d instead use of
I have a small app there is 3 seconds intro page, then the content
How to divide the #000000 - #ffffff interval intro x equal parts, to get
According to my professor in Intro. to Database Theory, there are NO examples in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.