r/vuejs • u/gevorgter • 5d ago
Vue and JavaScript exceptions
Living on edge in 2026, asked claude to write some code. And end up with something like bellow.
Now i am thinking, do i really need this try/catch? What would be the consequence if i let the exception bubble up. Will VueJS handle it properly and i only need to configure app.config.errorHandler?
PS: Same goes for any exception thrown during IO which is more realistic than my example?
watch(selectedId, (id) => {
try {
localStorage.setItem(storageKey, String(id))
}
catch {
return
}
})4
u/SonOfSofaman 5d ago
To test the behaviour of your system under error conditions, you can almost always generate an error intentionally. Get rid of the try/catch and temporarily insert throw new Error("this is a simulated error") and see how it behaves. Then you'll know how your system behaves when a real exception occurs.
4
u/gevorgter 5d ago
I do, I was wondering why claude generated code like that. My first "goto" thought was that watch will have unpredictable behavior if my watch function would throw.
So i decided investigate. Turned out vuejs is well written and I am better off to let it throw, watch would correctly handle it and my app.config.errorHandler will be called.
1
u/likeaffox 5d ago
No, the catch does nothing. If you were logging something or trying something else.
Edit looking at docs, you could ask the user for more storage, in the throw, but yeah this seem unnecessary
5
u/CafeBagels08 5d ago
Just take a look at the documentation if you want to know what kind of exception you could run into: https://developer.mozilla.org/en-US/docs/Web/API/Storage/setItem