Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to handle errors in Angular JS

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 281
    Comment on it

    Hello All,

    There are several ways to handle errors in Angular JS, Some of them are listed bellow:

    1. We can use $exceptionHandler which enables us to catch errors which can occur any where in or application. by default $exceptionHandler prints the error to the console log window, but we can create our own function to handle error.

      angular.module('myErrorHandler', [])
      .factory('$exceptionHandler', function () {
          return function geterrorHandler(exception, cause) {
              console.error(exception.stack);
              // do some thing
          };
      });
      

    2. we can use try can catch block which helps us to catch the exception at client side. Ex :

      function myFunction(){
          try {
               // do something
          }
          catch (e) { 
              // do something
          }
      }
      
    3. If we are using $http service for accessing data from server , we have to function success and error and in error function we can put or logic to handle error. Ex.

      $http({method: 'Get',url:'myPage/getData'
          }).success(function (data) {
              // do some thing
          }).error(function (data) {
            // do some thing
          });
      

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: