I have created a single page application using Angular2 framework. I have used typescript for creating Angular2 components.
While running the app,i am getting null value for a variable(x) in the AppComponent mentioned below.
When exception occurs, it stops the application execution without proceeding to the next line eventhough i have handled the error using Custom Errorhandler.
Please let me know if there is any solution to catch(without try/catch) the exception without blocking the application.
Error Handler service:
import { ErrorHandler, Injectable} from '@angular/core';
@Injectable()
export class CustomErrorHandler implements ErrorHandler {
constructor() { }
handleError(error) {
console.log('Handling the error');
}
}
App Module:
import { NgModule, ApplicationRef, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { CustomErrorHandler } from './error-handler';
import { ServicesModule } from './services';
import { AppComponent } from './app.component';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule],
bootstrap: [AppComponent],
providers: [
{
provide: ErrorHandler,
useClass: CustomErrorHandler
}
]
})
export class AppModule { }
App Component:
import {Component} from '@angular2/core';
@Component({
selector:'my-app',
template:'<p>Hello {{name}}</p>'
});
export class AppComponent{
constructor(){
}
OnInit(){
let x:any=getDataFromServer(); //assume x got data from the server
let z:any= x.data.value; //error occurs if x is null
let name="Hello";
}
}
Thanks in advance.