Why are 404s not triggering my 404.php template?

I found that the problem was with the otherwise very useful plugin Legacy URL Forwarding.

It uses the following function (abridged here) with the 404_template hook.

function doUrlForwarding() {

// make a $query based on the $_SERVER['REQUEST_URI']

// if a post is found, redirect to it
   if (!empty($query->post->ID)) {
      wp_redirect(get_permalink($query->post->ID), '301' );
   }
}

add_action('404_template','doUrlForwarding');

With the above hook active, the 404.php does not get fired. However, if I place doUrlForwarding at the top of 404.php, it works correctly.

This answers the mystery, but I still don’t know why using the
404_template hook is preventing the 404.php template from loading on
normal 404s. Comment if you have an idea.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)