No, there are no exceptions, and no formal way of registering an exit routine to run automatically upon termination.
However, you can get close if you do not need environment changes to persist after the script terminates. You can run the majority of your script in a new CMD session, and then you can have cleanup routines in the parent level that will always run upon termination as long as the console window is not killed. Control will be returned back to the outer shell if the inner script terminates normally, or terminates due to Ctrl-C, or terminates due to fatal syntax error, etc.
Here is a trivial demonstration:
@echo off
if "%~1" equ ":main" (
shift /1
goto main
)
cmd /d /c "%~f0" :main %*
echo Cleanup actions go here
exit /b
:main
echo Main actions go here
exit /b
UPDATE - Hacked exceptions
I have since developed an effective exception hack using nothing but native batch commands - See Does Windows batch support exception handling?.
Exceptions can be thrown at any level of your code, and the exception bubbles up until a catch block handles the exception. The code cannot recognize errors on its own - each exception must be explicitly thrown by your code. It also cannot respond to , fatal syntax errors, or closed console. But if those limitations are acceptable, then it is an extremely effective way of installing cleanup code.