JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Error handling in batch scripts

Author:JIYIK Last Updated:2025/03/20 Views:

Every scripting and programming language contains error handlers, for example Java contains try-catch for error handling. In batch scripts, there is no direct way to do this, but we can create error handlers in batch scripts using the built-in variable of the batch script name %ERRORLEVEL%.

This article will show you how to create a batch script to handle errors and failures. In addition, we will provide some examples to make the topic easier.


Error handling in batch scripts

When a command is executed successfully, it always returns an EXIT CODE indicating whether the command was executed successfully or failed. So, to create error handling in batch files, we can use this exit code in our program.

You can create an error handler in the following general format:

@Echo off
SomeCommand && (
  ECHO Message for Success
) || (
  ECHO Message for Failure or Error
)

We can also do this by checking %ERRORLEVEL%a variable called . If the variable contains a value that is not equal to 0, there may have been a problem or error while executing the command. To test the %ERRORLEVEL% variable, you can follow this example code:

@ECHO off
Some Command Here !!!
IF %ERRORLEVEL% NEQ 0 (Echo Error found when running the command &Exit /b 1)

You must note that the keyword NEQ stands for Not Equal To. The variable %ERRORLEVEL% will only contain a non-zero value if there is a problem or error in the code.

Example with error

Below, we have shared an example. We will run a batch file named Your_file.bat from a location.

We intentionally deleted the file from the directory. So this is a wrong command.

Our example code is:

@echo off
ECHO Running a Batch file
CD G:\BATCH\
CALL Your_file.bat
IF  errorlevel 1 GOTO ERROR
ECHO The file run successfully.
GOTO EOF

:ERROR
ECHO The file didn't run successfully.
CMD /k
EXIT /b 1

:EOF

Now, as the file does not exist in the directory, it will show error and when you run the code shared above, you will get the following output.

Output:

Running a Batch file
The system cannot find the path specified.
'Your_file.bat' is not recognized as an internal or external command,
operable program or batch file.
The file didn't run successfully.

An example of code that runs successfully without errors

In the above example, we intentionally made a mistake in the code to understand how the code works. If we correct it like this:

@echo off
ECHO Running a Batch file
CALL "G:\BATCH\Yourfile.bat"
IF  errorlevel 1 GOTO ERROR
ECHO The file runs successfully.
GOTO EOF

:ERROR
ECHO The file didn't run successfully.
CMD /k
EXIT /b 1

:EOF

Then we will get output like this:

Running a Batch file
This is from the first file
The file runs successfully.

Keep in mind that all the commands we discuss here only work in the Windows Command Prompt, or CMD environment.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial