JavaScript internal errors (exceptions)

Is there a preferred way to include internal exceptions when throwing exceptions in JavaScript?

I am relatively new to JavaScript based on C # background. In C # you can do the following:

try { // Do stuff } catch (Exception ex) { throw new Exception("This is a more detailed message.", ex); } 

In the samples that I saw in JavaScript, I could not find how to catch the exception, add a new message, and rethrow the new exception, still passing the original exception.

+6
source share
2 answers

You can throw away any object you want:

 try { var x = 1/0; } catch (e) { throw new MyException("There is no joy in Mudville", e); } function MyException(text, internal_exception) { this.text = text; this.internal_exception = internal_exception; } 

Then an error of type MyException will appear with the text and internal_exception properties.

+4
source

You can use: throw message; or throw new Error([message[, fileName[, lineNumber]]]);

For instance:

 try { throw new Error('Generic message'); } catch (ex) { throw "This is a more detailed message." + ex.message; } 
0
source

Source: https://habr.com/ru/post/977439/


All Articles