I want to build a node application and I want to handle the exception, in java we use exception class to handle exceptions,how can I do the same with node. Do I have any library for handling exception in node?
Asked
Active
Viewed 342 times
1 Answers
1
I would advise you to look into Promises, this is really powerful object which represents the eventual completion (or failure) of an asynchronous operation. This brings you really easy way to handle the errors.
In case you want to extend some Exception class, there is no point of doing it because JS is not strictly typed language as Java. Thus when you do catch you will be catching all the errors that can appear in a try block and you cannot specify which exception class you want to use unless you will be verifying the instance of the error which looks not good at all.
try {
throw new SpecificError;
} catch (e) {
if (e instanceof SpecificError) {
// specific error
} else {
throw e;
}
}
Have a look also into this.
And as GreenLAge4964 said, it is good to search.

Rufi
- 2,529
- 1
- 20
- 41
-
yes I am agree,but cant we use any library, like how we extend exception class in java application? – Hemmady Sep 14 '18 at 07:44
-
I have edited my answer. – Rufi Sep 14 '18 at 07:58