Skip to main content
InterviewHubPRO
Section Overview
Q376

Question 376 of 407

Part 9: React Hooks Deep Dive (262 - 316)

92%
React Core & Hooks (407 Qs)•Part 9: React Hooks Deep Dive (262 - 316)

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

junior LevelCode ExampleHooks

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.