InterviewHubPRO
Section Overview
Q413

Question 413 of 445

Part 11: Legacy Architecture, Migration Patterns & Historical Internals (354 - 445)

93%
React & Modern Frontend (445 Qs)•Part 11: Legacy Architecture, Migration Patterns & Historical Internals (354 - 445)

What is the difference between try catch block and error boundaries?

Try catch block works with imperative code whereas error boundaries are meant for declarative code to render on the screen.

arduino
For example, the try catch block used for below imperative code
javascript
    try {
      showButton();
    } catch (error) {
      // ...
    }
lua
Whereas error boundaries wrap declarative code as below,
javascript
    <ErrorBoundary>
      <MyComponent />
    </ErrorBoundary>
vbnet
So if an error occurs in a **componentDidUpdate** method caused by a **setState** somewhere deep in the tree, it will still correctly propagate to the closest error boundary.