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
    • 293
    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.

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

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

      1. function myFunction(){
      2. try {
      3. // do something
      4. }
      5. catch (e) {
      6. // do something
      7. }
      8. }
    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.

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

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: