Reputation: 15008
I wrote the following code in Angular 2 project:
ngOnInit () {
try {
this.http.request("http://29.media.tumblr.com/ZabOTt2mpdp8ljaxp88lwdhP_400.jpg").subscribe((res: Response) => {
console.log(res.url);
this.urls.push(res.url);
});
} catch(e) {
console.log("Error");
}
}
Well, the path "http://29.media..." is broken. I'd expect that the code inside the try block creates an error and then the code inside catch block will be activated. But no. What I got is an Error. Do you know why?
By the way, there is no Same-Origin-Policy issue here.
Upvotes: 0
Views: 113
Reputation: 2822
Because it is async like a promise. The function has already returned before any exception is thrown.
You can use
...subscribe(
res => {
},
err => {
})
Upvotes: 1