Improve this Doc View Source $exceptionHandler

From Get docs
< Service components in ngAngularjs/docs/1.8/api/ng/service/$exceptionhandler

Improve this Doc View Source $exceptionHandler

  1. service in module ng


Any uncaught exception in AngularJS expressions is delegated to this service. The default implementation simply delegates to $log.error which logs it into the browser console.

In unit tests, if angular-mocks.js is loaded, this service is overridden by mock $exceptionHandler which aids in testing.


The example below will overwrite the default $exceptionHandler in order to (a) log uncaught errors to the backend for later inspection by the developers and (b) to use $log.warn() instead of $log.error().

  module('exceptionOverwrite', []).
  factory('$exceptionHandler', ['$log', 'logErrorsToBackend', function($log, logErrorsToBackend) {
    return function myExceptionHandler(exception, cause) {
      logErrorsToBackend(exception, cause);
      $log.warn(exception, cause);

Note, that code executed in event-listeners (even those registered using jqLite's on/bind methods) does not delegate exceptions to the $exceptionHandler (unless executed during a digest). If you wish, you can manually delegate exceptions, e.g. try { ... } catch(e) { $exceptionHandler(e); }



$exceptionHandler(exception, [cause]);


Param Type Details
exception Error Exception associated with the error.



string Optional information about the context in which the error was thrown.

© 2010–2020 Google, Inc.
Licensed under the Creative Commons Attribution License 3.0.$exceptionHandler