为什么在这种情况下使用try / catch是个坏主意?

时间:2022-08-28 09:06:51

I've been working on hooking up mongoDB to a node.js server. I've got the code all neatly put away, however it takes about 5~ seconds to connect, and if a request for an insert or a query comes before that, the server will crash.

我一直在努力将mongoDB连接到node.js服务器。我已经把代码整齐地放好了,但连接需要大约5秒钟,如果在此之前有插入或查询请求,服务器将崩溃。

My first instinct was to use try/catch to filter off any requests which error out. I will want the server to go on regardless of what errors an individual request breaks, so why not use it?

我的第一直觉是使用try / catch来过滤掉任何错误的请求。我希望服务器继续运行,无论单个请求中断了什么错误,为什么不使用它呢?

Everywhere I look it's touted as a bad idea, I'm not sure I understand why.

在任何地方我都认为这是一个坏主意,我不确定我理解为什么。

1 个解决方案

#1


3  

A try/catch block around something that simply ignores the error is generally considered bad practice. However, if that is the behavior you want, then there is nothing wrong with it. Just consider that it may not actually be the best behavior. You may, at a minimum, want to log the fact that an exception occurred.

围绕忽略错误的东西的try / catch块通常被认为是不好的做法。但是,如果这是您想要的行为,那么它没有任何问题。只要考虑它实际上可能不是最好的行为。您至少可以记录发生异常的事实。

Now, due to the asynchronous nature of Node.js, try/catch blocks are sometimes not useful. I don't know what part of the MongoDB API you are using, but if there is a callback, you will want to instead check the err parameter, which should be the first parameter of your callback function in most cases.

现在,由于Node.js的异步特性,try / catch块有时没用。我不知道您正在使用的MongoDB API的哪个部分,但是如果有回调,您将需要检查err参数,在大多数情况下,该参数应该是回调函数的第一个参数。

Finally, for all of my applications, I connect to any necessary DBs synchronously, and start listening on ports afterwards. But, this is only relevant if a persistent connection makes sense for your project. Plus, you still have to watch for errors, which can occur as connection failures do happen.

最后,对于我的所有应用程序,我同步连接到任何必要的数据库,然后开始侦听端口。但是,只有持久连接对您的项目有意义时,这才有意义。此外,您仍然需要注意错误,这可能会在连接失败时发生。

#1


3  

A try/catch block around something that simply ignores the error is generally considered bad practice. However, if that is the behavior you want, then there is nothing wrong with it. Just consider that it may not actually be the best behavior. You may, at a minimum, want to log the fact that an exception occurred.

围绕忽略错误的东西的try / catch块通常被认为是不好的做法。但是,如果这是您想要的行为,那么它没有任何问题。只要考虑它实际上可能不是最好的行为。您至少可以记录发生异常的事实。

Now, due to the asynchronous nature of Node.js, try/catch blocks are sometimes not useful. I don't know what part of the MongoDB API you are using, but if there is a callback, you will want to instead check the err parameter, which should be the first parameter of your callback function in most cases.

现在,由于Node.js的异步特性,try / catch块有时没用。我不知道您正在使用的MongoDB API的哪个部分,但是如果有回调,您将需要检查err参数,在大多数情况下,该参数应该是回调函数的第一个参数。

Finally, for all of my applications, I connect to any necessary DBs synchronously, and start listening on ports afterwards. But, this is only relevant if a persistent connection makes sense for your project. Plus, you still have to watch for errors, which can occur as connection failures do happen.

最后,对于我的所有应用程序,我同步连接到任何必要的数据库,然后开始侦听端口。但是,只有持久连接对您的项目有意义时,这才有意义。此外,您仍然需要注意错误,这可能会在连接失败时发生。