![]() |
VOOZH | about |
In AngularJS, a service is a function or object that is available for dependency injection (DI) in an AngularJS app. Services are typically used to encapsulate and reuse business logic and other app functionality that is not directly related to the presentation of data in the app.
The $exceptionHandler service is a built-in AngularJS service that can be used to handle exceptions thrown during the execution of an AngularJS app. By default, AngularJS logs uncaught exceptions to the browser's console and displays an error message to the user.
Syntax:
app.factory('$exceptionHandler', function() {
return function(exception, cause) {
// Custom exception handling logic here
};
});
Parameters: The $exceptionHandler service is a function that takes two parameters:
An exception handler is a service or component that is designed to handle exceptions or errors that may occur in an application. Here are a few examples of what an exception handler service might do:
Example 1: This example describes a sample error that is made to display on the console using the $exceptionHandler Service.
Output:
Example 2: In this example, the myCtrl controller has a calculate() function that calculates the square root of a number input by the user. If the user enters a negative number, an exception is thrown with the message "Number must be positive." The exception is caught by the catch block and the error message is displayed in the HTML template using the errorMessage model. The exception is also passed to the $exceptionHandler service, which can be used to log or handle the exception in some other way.
Output:
Reference: https://docs.angularjs.org/api/ng/service/$exceptionHandler