Angularjs Examples

AJS Examples


AngularJS $exceptionHandler Service

The AngularJS $exceptionHandler is used to handle any uncaught exception in angular expressions. The default implementation of error is using $log.error in the browser console. It allows overriding the default exception handler behaviour by redefining in a factory.

Syntax

$exceptionHandler(exception, [cause]);

AngularJS $exceptionHandler Service Example

<!DOCTYPE html>        
<html>        
<head> <!-- www.techstrikers.com -->        
<title>AngularJS $exceptionHandler Example</title>        
<script SRC="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>   
 <script> 
    var app = angular.module("ExceptionInitialization",[]);
       		app.factory('$exceptionHandler',function(){
			return function (exception, cause){
				alert('Error occurred');
			};
		});
		app.controller("ExceptionHandlerController",function ($scope) {
          $scope.error = "";
          $scope.ThrowError = function()
			{
				throw new Error('exception occurre');
			}		  
        });
   
</script>        
</head>        
<body style="background-color:#DDE4E9;font-family:arial;" ng-app="ExceptionInitialization">        
  <fieldset style="background-color:#DDE4E9;">          
    <legend>AngulerJS $exceptionHandler Example</legend>          
<div ng-controller="ExceptionHandlerController">  
	<p><button ng-click="ThrowError()">Throw Error</button></p>
     </p>
  
    </div>
      </fieldset>         
</body>        
</html>  
See Live Example