You need an error handler, preferably one which is specific to your purposes. In this case, it doesn't need to do a thing in the world but restore your saved "lay" variable.
This has been discused in detail dozens of times, and many examples have been posted.
A very simple form would be:
(defun c:DL (/ *error* lay sc ds layname )
(defun *error* (msg)
(setvar "clayer" lay)
(princ))
(setq lay (getvar "clayer"))
(setq sc (itoa (getvar "useri1")))
(setq ds (strcat "DyerDim" sc))
(setq layname (strcat "05-Dim" sc))
(command "layer" "make" layname "color" "2" "" "")
(Command "-dimstyle" "restore" ds)
(Command "dimlinear")
(while (< 0 (getvar "cmdactive"))
(command pause)
)
(setq dim (strcat "Dimension created using " ds " with layer " layname))
(prompt dim)
(*error* nil)
)
Note that all of your variables should be declared local as shown.