简体   繁体   English

Javascript:错误处理

[英]Javascript: error handling

I'm new to javascript, and I'm wondering how I should handle errors. 我是javascript新手,我想知道如何处理错误。 More precisely, when should I use exceptions, return values, callbacks or promises ? 更准确地说,何时应该使用异常,返回值,回调或Promise? I'm currently using return values for synchronous functions, and callbacks/promises for asynchronous functions, but I'm not sure that's a good choice, because I never use exceptions, and I know some people consider that they are very useful, especially when a synchronous function could return usual error values (-1 and null) as correct values, but in practice, it's very rare that -1 and null are both correct results for a function . 我目前正在将返回值用于同步函数,并将回调/承诺用于异步函数,但是我不确定这是一个好选择,因为我从不使用异常,而且我知道有人认为它们非常有用,尤其是当同步函数可以返回通常的错误值(-1和null)作为正确值,但是实际上,-1和null都是函数的正确结果是非常罕见的。 Someone could tell me how to choose the right solution ? 有人可以告诉我如何选择正确的解决方案?

Problem with return values is not that there isn't values to choose from but the fact that you need to manually check for them and propagate them down the stack. 返回值的问题不是没有可供选择的值,而是您需要手动检查它们并将它们传播到堆栈中的事实。

If an error happens through an exception and you don't have a try-catch anywhere, you will crash the process and see a nice stack trace. 如果由于异常而发生错误,并且您在任何地方都没有try-catch,则将导致进程崩溃并看到一个不错的堆栈跟踪。 The error can either be a bug in code (typically TypeError and ReferenceError) or an expected occurrence like file not existing. 该错误可能是代码中的错误(通常是TypeError和ReferenceError),也可能是文件中不存在的预期错误。 In latter case you should add try-catch and handle the file not existing, in former case you should not add try-catch but fix the bug instead. 在后一种情况下,您应该添加try-catch并处理不存在的文件,在前一种情况下,您不应添加try-catch而是修复错误。

On the other hand, if you forget to check for an erroneous return value, then the program will probably silently continue in undefined state or some other worse outcome than crashing. 另一方面,如果您忘记检查错误的返回值,则程序可能会在未定义的状态下静默地继续运行,或者比崩溃更糟糕。 There is also the fact that bugs are usually caused by forgetting something so needing to explicitly check return values everywhere is bad. 还有一个事实,就是错误通常是由于忘记了某些东西造成的,因此需要在任何地方显式检查返回值都是很糟糕的。

Another issue is that even if you check for a return value, you might not know at that point how to handle it so you need to manually propagate it further which again requires manual code. 另一个问题是,即使您检查返回值,此时您可能也不知道如何处理它,因此您需要进一步手动传播它,这又需要手动代码。 With try-catch, the try catch doesn't need to there right now, it can be further down the stack where the error can be handled (like displaying message in UI that "something went wrong"). 使用try-catch时,try-catch不需要立即到那里,它可以在可以处理错误的堆栈的更下方(例如在UI中显示“出了点问题”的消息)。

If you want to handle errors with asynchronous code then you should use promises. 如果要使用异步代码处理错误,则应使用promise。 Although a minimal promise implementation is still unusable for this, it is still light years ahead of error handling asynchronous code with callbacks which is ridiculous and doesn't only require manual checks and propagations but requires 2 different channels of error handling: synchronous and asynchronous. 尽管最小承诺实现仍无法使用,但与带回调的错误代码异步处理错误相比仍遥遥领先,这是荒谬的,不仅需要手动检查和传播,还需要两种不同的错误处理通道:同步和异步。 See What are promises and why should I use them? 请参阅什么是承诺,为什么要使用它们? (Disclaimer: I am the author) (免责声明:我是作者)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM