0
0
Vueframework~20 mins

Error boundaries with onErrorCaptured in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Error Boundary Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a child component throws an error inside onErrorCaptured?

Consider a Vue 3 component using onErrorCaptured to catch errors from its child components. What will be the rendered output if the child throws an error?

Vue
<script setup>
import { ref, onErrorCaptured } from 'vue'
const errorMessage = ref('')
onErrorCaptured((err) => {
  errorMessage.value = 'Error caught: ' + err.message
  return false
})
</script>
<template>
  <div>
    <ChildComponent />
    <p v-if="errorMessage">{{ errorMessage }}</p>
  </div>
</template>
AThe entire component crashes and nothing renders.
BThe error is ignored and nothing is displayed.
CThe error message is displayed below the child component.
DThe error message is logged but not shown in the UI.
Attempts:
2 left
💡 Hint

Think about what onErrorCaptured does and how returning false affects error propagation.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses onErrorCaptured to prevent error propagation?

Choose the correct syntax for onErrorCaptured that catches an error and stops it from propagating further.

AonErrorCaptured((error) => { console.log(error); return true })
BonErrorCaptured(error => { console.log(error) })
ConErrorCaptured(error => { console.log(error); throw error })
DonErrorCaptured((error) => { console.log(error); return false })
Attempts:
2 left
💡 Hint

Remember that returning false stops propagation.

🔧 Debug
advanced
2:00remaining
Why does the error not get caught by onErrorCaptured in this code?

Given this code snippet, why does the error thrown in ChildComponent not get caught by onErrorCaptured in the parent?

Vue
<script setup>
import { onErrorCaptured } from 'vue'
onErrorCaptured((err) => {
  console.log('Error caught:', err.message)
  return false
})
</script>
<template>
  <ChildComponent />
</template>

<script>
export default {
  components: {
    ChildComponent: {
      setup() {
        throw new Error('Oops!')
      },
      template: '<div>Child</div>'
    }
  }
}
</script>
ABecause onErrorCaptured only catches errors in template rendering, not setup.
BBecause the error is thrown during setup, before onErrorCaptured is registered.
CBecause the parent component does not return false in onErrorCaptured.
DBecause the error is caught but immediately rethrown.
Attempts:
2 left
💡 Hint

Consider the lifecycle order of setup and error capturing.

state_output
advanced
2:00remaining
What is the value of errorCount after multiple errors caught by onErrorCaptured?

In this Vue component, errorCount increments each time an error is caught by onErrorCaptured. After three child components throw errors, what is the final value of errorCount?

Vue
<script setup>
import { ref, onErrorCaptured } from 'vue'
const errorCount = ref(0)
onErrorCaptured(() => {
  errorCount.value++
  return false
})
</script>
<template>
  <ChildOne />
  <ChildTwo />
  <ChildThree />
</template>
A3
B0
C1
DThrows an error and does not update
Attempts:
2 left
💡 Hint

Each error triggers the onErrorCaptured hook once.

🧠 Conceptual
expert
2:00remaining
Which statement about onErrorCaptured is true?

Choose the correct statement about Vue's onErrorCaptured hook.

A<code>onErrorCaptured</code> can prevent error propagation by returning false, but errors in asynchronous code inside child components are not caught.
B<code>onErrorCaptured</code> must be declared in the root component to catch any errors.
C<code>onErrorCaptured</code> only catches errors in parent components, not children.
D<code>onErrorCaptured</code> catches all errors including those in asynchronous code and stops propagation by default.
Attempts:
2 left
💡 Hint

Think about error capturing scope and async errors.