0
0
Angularframework~20 mins

catchError for error handling in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
RxJS Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when catchError returns EMPTY in an Angular service?

Consider an Angular service method that uses catchError(() => EMPTY). What will the subscriber receive if an error occurs?

Angular
import { EMPTY } from 'rxjs';
import { catchError } from 'rxjs/operators';

this.http.get('/api/data').pipe(
  catchError(() => EMPTY)
).subscribe({
  next: data => console.log('Data:', data),
  error: err => console.log('Error:', err),
  complete: () => console.log('Completed')
});
AThe subscriber receives no data, no error, and the observable completes silently.
BThe subscriber receives an empty array as data and completes.
CThe subscriber receives an error notification with the original error.
DThe subscriber receives undefined as data and continues listening.
Attempts:
2 left
💡 Hint

Think about what EMPTY observable does in RxJS.

📝 Syntax
intermediate
2:00remaining
Identify the correct catchError usage to return a fallback value

Which option correctly uses catchError to return a fallback observable emitting the string 'fallback' on error?

Angular
import { of } from 'rxjs';
import { catchError } from 'rxjs/operators';

this.http.get('/api/data').pipe(
  // catchError usage here
).subscribe(data => console.log(data));
AcatchError(() => { return 'fallback'; })
BcatchError(() => of('fallback'))
CcatchError(() => 'fallback')
DcatchError(() => of(fallback))
Attempts:
2 left
💡 Hint

The fallback must be an observable, not a plain value.

🔧 Debug
advanced
2:00remaining
Why does this catchError not handle the error as expected?

Given this code snippet, why does the error still propagate to the subscriber's error handler?

Angular
this.http.get('/api/data').pipe(
  catchError(err => {
    console.error('Error caught:', err);
    throw err;
  })
).subscribe({
  next: data => console.log('Data:', data),
  error: err => console.log('Subscriber error:', err)
});
ABecause rethrowing the error inside catchError causes the observable to error again, propagating to subscriber.
BBecause catchError must return a Promise to handle errors properly.
CBecause catchError only catches synchronous errors, not HTTP errors.
DBecause the error is swallowed and never reaches the subscriber.
Attempts:
2 left
💡 Hint

What happens if you throw inside a catchError callback?

state_output
advanced
2:00remaining
What is the output sequence when catchError returns a fallback observable?

What will be logged to the console when this code runs?

Angular
import { of, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';

of('start').pipe(
  catchError(() => of('fallback'))
).subscribe({
  next: val => console.log('Next:', val),
  error: err => console.log('Error:', err),
  complete: () => console.log('Complete')
});

throwError(() => new Error('fail')).pipe(
  catchError(() => of('fallback'))
).subscribe({
  next: val => console.log('Next:', val),
  error: err => console.log('Error:', err),
  complete: () => console.log('Complete')
});
A
Next: start
Error: fail
Next: fallback
Complete
B
Next: start
Next: fallback
Complete
Complete
C
Next: start
Complete
Next: fallback
Complete
D
Error: fail
Next: fallback
Complete
Complete
Attempts:
2 left
💡 Hint

Remember that of emits values and completes, and throwError emits an error.

🧠 Conceptual
expert
2:00remaining
Why is catchError preferred over try-catch for handling HTTP errors in Angular?

In Angular services using RxJS, why should you use catchError instead of a traditional try-catch block to handle HTTP errors?

ABecause HTTP calls are asynchronous and return observables, so errors must be handled in the observable stream using catchError.
BBecause try-catch blocks only work with synchronous code and cannot catch errors from observables.
CBecause catchError allows you to recover from errors and continue the observable stream, unlike try-catch.
DAll of the above.
Attempts:
2 left
💡 Hint

Think about how asynchronous code and observables work in Angular.