Does try catch adds a lot of overhead on the system?

Some time ago I read on this forum to not to use try catch (or try recue, I dont remember now). So wich one is recomended to use?

Fff topic because I do not want to polute the main post. I usually dont even use try catch, only with to chain functions, but I have to do a technical test for a typescript funcional role, and it’s difficult not to have with to group function results. I could do something similar with try catch and throwing a object error on the function called on the try. I think probably try catch works equal on both languages, so I just want to know if it’s a good patern to follow overall since I got no with keyword to work with

Is this question about typescript or elixir?

The docs explain it well. TL;DR: catch catches throws which is a way around the lack of early returns (if you really must). rescue rescues exceptions as well as explicitly raised exceptions. When to use this is a bigger topic related to “let it crash” and error control flow. I really only ever explicitly rescue in mix tasks where the lifetime is very short and an error should bail and bring everything down. I just use rescue to add a “template” to the error message. I’m sure there are other legit situations but I don’t have any other examples off the top of my head.

Sorry if this isn’t answering your question very well.

2 Likes

Just a quick comment: both try/catch and try/rescue are implemented using the underlying Erlang try/catch. So there is no benefit in using one over the other, it all depends what you want to catch.

6 Likes

about try catch and rescue, I just posted here bcs it’s the forum I most use

As far as I understand, the throw is a little more lighter on resource usage because a stacktrace is not captured.

No, you can just use __STACKTRACE__ with catch as well.

It’s lighter because the errors are not converted to Elixir exceptions.

Edit: it’s lighter by a tiiiiiny amount, not worth optimising in general.

1 Like

Ah okay.