Home > OS >  Keep JS from creating exception objects
Keep JS from creating exception objects

Time:09-27

So i have situation where I have to save the solution from a giant formula (calculation) to a variable and i know for sure that this will often have no solution so

I will use a try catch clause. And I can not calculate it twice because it would be, among possible other things, much too long (timewise).

But from what I know, with every catch, an exception object (etc) will get created. Is it possible to keep this from happening (some command or similar)?

Because I know for sure I will never need these because I know why it is impossible sometimes, but this automatic behavior would obviously cause unnecessary stack and processor (and that way runTIME) -usage.

Or is this default behavior not the case for js?

CodePudding user response:

But from what I know, with every catch, an exception object (etc) will get created...

That's incorrect. An Error object will only be created if an error is thrown, not just because you have a catch block in your code. (And technically, in JavaScript, you can throw other things, they don't have to be Error instances, though when the browser or JavaScript engine throws something, it's always an Error object [directly or indirectly via a subclass].) If an error is created and thrown, and your code doesn't keep it, it'll immediately be eligible for garbage collection anyway. Creating and garbage collecting a single object is not a significant effort.

  • Related