DEV Community

Cover image for Custom exceptions with JS

Custom exceptions with JS

Damian Cipolat on March 06, 2020

It is very common languages ​​such as java and c # to create custom exceptions, to differentiate error situations from one another. In JS there is ...
Collapse
 
tieppt profile image
Tiep Phan

You don't need explicit call this line

class ValidationError extends Error {
  constructor(message) {
    super(message) // this one will store the message variable to message field
    this.name = 'VALIDATION_ERROR'
    // this.message = message
  }
}
Collapse
 
khuongduybui profile image
Duy K. Bui
if (!exception)
    throw exception();

Did you mean !!exception?

Collapse
 
danielpdev profile image
danielpdev • Edited

Good article!
It's easy to just throw error and use catch to handle the exception, but ideally you shouldn't use throw very often as it's a huge drawback on performance.

Collapse
 
sqlrob profile image
Robert Myers

The way I've always heard, and continue to follow, exceptions are for exceptional conditions. They shouldn't be for logic flow.

In a good many of my projects (all languages), there's plenty of throws, but almost no catches at all, except maybe at or near the top level. In the case of errors, I want things to blow up. If the db goes down, there's not much I can do. If I have a bug that results in a throw, it's noisy and I have a nice stack trace right to it, fail early and noisy.

Collapse
 
damxipo profile image
Damian Cipolat • Edited

Well I'm using this way in a very massive production project and we don't lose Performance. Do you have a link that supports this or what you say or experience to tell?

Collapse
 
danielpdev profile image
danielpdev

try-catch will impact your performance only if you actually throw exceptions.
Examples:

  1. Performance loss when exception is thrown: jsperf.com/try-catch-performance-jls
  2. No performance loss if no exception is thrown: jsperf.com/try-catch-performance-j...

Conclusion:
I'm not saying that you should not use try-catch at all, but like in the examples above actually throwing an exception can be easily avoided.

Thread Thread
 
damxipo profile image
Damian Cipolat

Nice, thanks for the links provides very useful information. I will be investigating about this.

Collapse
 
renatoruk profile image
Renato Ruk

Can you explain this in more detail? I guess throwing a lot of exceptions frequently may introduce performance issues, but not sure why. Is it because of the stack tracing?

Collapse
 
danielpdev profile image
danielpdev

Yes, you are right. It's because of the stack tracing. As it needs to build your stack trace and it walks through the call stack to collect all required information like method names, line numbers etc.

Collapse
 
damxipo profile image
Damian Cipolat

Maybe the problem Is hoy don't catch the exceptoins very well.Seriously, it's the 1st time I've heard that throwing exceptions loses Performance. but I want to see how you base it, I don't say no but I am working like this on a project and we didn't lose performance

Collapse
 
bernardbaker profile image
Bernard Baker

Good article.

Collapse
 
okyanusoz profile image
okyanusoz

Thanks!

Collapse
 
monfernape profile image
Usman Khalil

Loved it.