If you look in the EE source (v.2.10.1) you will see the following in system/expressionengine/libraries/Auth.php
(line numbers on the left):
156 if ($authed === FALSE)
157 {
158 @header('WWW-Authenticate: Basic realm="'.$realm.'"');
159 ee()->output->set_status_header(401);
160 @header("Date: ".gmdate("D, d M Y H:i:s")." GMT");
161 exit("HTTP/1.0 401 Unauthorized");
162 }
EE's authentication hijacks the 401 response, dumping out a header and using PHP's exit()
function to dump out an "helpful" text string. AFAIK, and based on this evidence, there is no way to catch a 401 HTTP error code in a template, or even a raw Apache directive, without hacking the EE source, which is not recommended.
I have submitted this as a bug to Ellis Labs but have no heard anything back yet. If I do, I will add it to my own SO question that is similar to this.