python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
$exceptionHandler

AngularJs Exception/Error Handling using $exceptionHandler

Hello everyone, I am going to share the AngularJs exception or error handling using  $exceptionHandlerThe AngularJs $exceptionHandler  is use to catch and handle the exceptions or errors. The details as given below.


The HTML Code-Sample

<div ng-app="exceptionApp" ng-controller="ExceptionCtrl"></div>

The AngularJs Code-Sample

var app = angular.module('exceptionApp', []);
app.provider({
    $exceptionHandler: function () {
        var errorHandler = function (ex) {
            alert(ex);
            //console.log(ex);
        };

        this.$get = function () {
            return errorHandler;
        };
    }
});

app.controller('ExceptionCtrl', ["$scope", "$exceptionHandler",
 function ($scope, $exceptionHandler) {
    $scope.name = "Anil Singh";

    //Throw error here using handler as function.
    $exceptionHandler.handler('Throw error');
}]);

The Live demo (HTML + AngularJs) code-sample

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>$exceptionHandler in AngularJs</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.min.js"></script>
    <script src="https://code.angularjs.org/1.4.7/angular-messages.js"></script>
    <script>
        var app = angular.module('exceptionApp', []);

        app.provider({
            $exceptionHandler: function () {
                var errorHandler = function (ex) {
                    alert(ex);
                    //console.log(ex);
                };

                this.$get = function () {
                    return errorHandler;
                };
            }
        });

        app.controller('ExceptionCtrl', ["$scope", "$exceptionHandler", function ($scope, $exceptionHandler) {
            $scope.name = "Anil Singh";

            //Throw error here using handler as function.
            $exceptionHandler.handler('Throw error');
        }]);
    </script>
</head>
<body ng-app="exceptionApp" ng-controller="ExceptionCtrl">
    <h1>Use of $exceptionHandler in AngularJs</h1>
    <p>Hello {{name}}!</p>
</body>
</html>


The output: go to link http://embed.plnkr.co/x6bqJg/preview

ANIL SINGH

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions