L
L
likejavascript2011-09-17 00:17:18
JavaScript
likejavascript, 2011-09-17 00:17:18

js. Central error handling

Guys share your experience on how to properly handle exceptions in JS applications. My idea is to put the entire application in a try/catch block and centrally handle the exceptions that occur. Suppose an exception of this type is sent in some function of my application: Accordingly, in the catch handler, the processing of the received exception is done: In general, I would like to know any exception handling patterns, or hear good advice. And please tell me which is better: to have only one try / catch block or many - at different levels? Thank you.

throw {
code:304,
name: 'ErrorType'
msg:'Ошибка.....',
file:'*.js',
line:115,
func: function(){}
}


catch(o){
// Можно записать ошибку в БД
log(....);
// Вызвать функцию обработчик (если есть)
o.func();
.....
// Ну и т.д
}


Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alienator, 2011-09-17
@likejavascript

No one will decide what is best for you.
Of course, you can and should have try/catchat the highest level. And give it some useful behavior - write it to the log, display a beautiful window, etc., so as not to frighten the user with system messages.
This is the necessary minimum.
And then look deeper. What will the handler function give you inside the exception object? In many cases it is already useless; it is too far from the point of occurrence of the error to try to fix it (for example, call the code again with a default value, wait for the resource to be freed, etc., in short, some kind of while/try).
Before each function call that might throw an exception, before each component entry, you have three options:

  • handle some (or all) exceptions here
  • process and pass above (re-throw)
  • do nothing (pass immediately upstairs)

You will have to omit exception handling at least where the finallycode is needed. And where you can do something meaningful before completely falling off screaming into the log.
And there, deeper, inside the component, a number of reasonable recommendations are already in effect:
1. Do not catch everything in a row. Handle only those exceptions that you know what to do with. If not entirely known, do a re-throw.
2. re-throw do it carefully. It is not necessary, again, to grab more from greed, and then skip the exceptions of unnecessary classes. For a Java script, this is expressed as follows:
Not good:
try {
     // ...
} catch (e) {
     if (! e instanceof MyError) {
          throw e;
     }
    // ...
}

Good:
try {
     // ...
} catch (e if e instanceof MyError) {
    // ...
}

3. What you do in finally is more important than what you do in catch. There is something to clean up - you need to clean it up.
4. No need to lose information about the error (generate or re-generate an exception of a wider class). True, when developing either, it’s better to keep your little things in yourself and replace low-level errors with more general ones.
Well, what I wanted to say, I said. Good luck.

G
gro, 2011-09-17
@gro

JS application is based on event handling, how do you put it in one try-catch?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question