r/vuejs • • 1d 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
    }
  })
2 Upvotes

16 comments sorted by

5

u/SonOfSofaman 1d 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 1d 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.

6

u/CafeBagels08 1d 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

3

u/heytheretaylor 1d ago

That’s really helpful. Learn something now everyday

0

u/gevorgter 1d ago

That was not the question. Question was about removing try/catch and consequences of doing that.

Will VueJs handle exception in a watch correctly. Normally watch handlers need to do cleanup

5

u/CafeBagels08 1d ago

A standard programming practice would be to handle exceptions at the near where the exception is raised. The best place would then be to handle it in your watch function. If you do no handle it there, it will call the hook onErrorCaptured() of all parent components or it will call the errorHandler of your Vue project. Your watch function won't be cleaned and it will be called again when the selectedId changes.

https://vuejs.org/api/composition-api-lifecycle.html#onerrorcaptured

-1

u/gevorgter 1d ago

That's is actually not correct, standard practice is to catch exception where you can meaningfully do something with that information. Normally you let it bubble up all the way to the top of your call chain and log it there.

6

u/WisdomSky 1d ago

"normally you let it bubble up all the way to the top"

says who? you?
wtf you mean normally? I've been developing web apps for more than a decade and has been into vue since 2014. that the thing you're saying is not normal. you do not NORMALLY let it bubble/propagate to to the top, you catch it as soon as close as possible to the source, so you can narrow the source of exception down and make it easier to debug or add gracefuly handlers/fallbacks when the desired action fails.

-3

u/gevorgter 1d ago

Exceptions carry stack trace, so you will know where it happened even if you let it bubble up.

1

u/WisdomSky 9h ago

and? what's your point for stating that?
do you even know what happens to your code when you don't handle/catch the exceptions as soon as possible?

Unhandled exceptions in the middle of a code execution can literally cause all of sorts of problems.

Did you really just think that your code will continue too execute once it encounters an exception?

0

u/gevorgter 2h ago

my 2 cents, try programming without wrapping everything into try/catch

  • Create some global function showError that would show error on a screen (dialog or toaster...)
  • TypeScript/Javascript have "using", use it.
  • Create your own Error class "class AppError extends Error {..}" to differentiate between unexpected exception (like IO, internet died suddently) and your "logic" exception when you need to terminate call chain
  • Override app.config.errorHandler in main.ts (if you are using vuejs) to show errors and output stack trace to console. (ask chatgpt how).
  • use async/await (if you do not already).

And your code will be much easier to read/maintain.

1

u/CafeBagels08 1h ago

So how do you manage exceptions if you let them bubble all the way up? Reporting is good, but it's not the same as managing it

1

u/gevorgter 33m ago

Exception handling is the most "complicated" topic in programming. Pretty much any app should have uniformed approach to handling exceptions. And any good framework such as VueJS would help in that.

Exceptions are NOT for logic flow control (usually). Exceptions are (usually) logic terminating exception aka non-recoverable. For example, Method1->Method2->Method3->Method4. If exception is thrown in Method4 there is no point to finish Method2, Method3, Method1. If you lost internet connection you most likely will not even be able to finish those methods. If you have a bug in a code and attempted division by 0 it's better to terminate rather than proceed with some corrupted data. BUT those methods are responsible for proper clean up even if exception is thrown. For that we have "using" and VueJS helps too (see onErrorCaptured). Most of the clean up is done automatically by framework.

Exception can happen unexpectedly, computer lost internet connection or bug in a code. Or exception can be triggered by the code. Let say deep down in a logic you realize that user is doing something he can not (should not) do. For those i recommend creating your own type like AppError above to be able to differentiate those. For example, If you caught your exception LoginNeededError you simply redirect to Login page and no need to log it.

Normally exception is caught on the top of your logic chain, in my example Method1, analyzed and dealt with it.

In case of VueJS the Method1 is a VueJS framework itself so you just use "app.config.errorHandler", so if you get LoginNeededError in your errorHandler method you redirect to login page. This code is writted only once and after that each method that needs to guard access to resources simply throws "new LoginNeededError" exception.

---------------------------------

With all that said, sometimes exceptions are recoverable, let say you have 2 credit card processors. One could not charge credit card because processor's server is down and you are simply attempting to charge with another processor. But those cases must be wrapped into some class that hides this. Your frontend usually should not care if you have 1 credit card processor or 2, It cares if card was charged or not. And those cases are rare.

So although there is not golden rule, properly written application with well though out exception handling would have handful of try/catch statements with errorHandler you will not even have single try/catch (vueJs has it for you).

→ More replies

1

u/CafeBagels08 1d ago

Reporting it to your logger is usually more of a last resort and something I would advise against. In this case, it's quite predictable, but for other cases, it could result in a behaviour you haven't fully planned for. Keeping your code predictable by limiting the number of possible branches is a good behaviour.

You are right about catching the exception where tou can meaningfully do something with that information. I would also add that catching it ASAP when you do something meaningful with it instead of letting it bubble up all the way to the top is preferable in most cases. For instance, in this particular case, if you catch that the localStorage is full, then call a function that clears the old keys and are no longer being used anywhere in your app to hopefully make enough room for the new keys. If that still doesn't work, it can then call your logger by indicating how much space is being taken in the user's local storage and that it is over the limit of what his browser can handle. That log will then be sent to Sentry or whatever error reporting system that you're using

1

u/likeaffox 1d 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