> For the complete documentation index, see [llms.txt](https://lodash.shujuwajue.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lodash.shujuwajue.com/util/attempt.md).

# \_.attempt

**语法：**

```javascript
_.attempt(func, [args])
```

**源代码链接：**[source](https://github.com/lodash/lodash/blob/4.17.10/lodash.js#L15246)

**npm包链接：**[npm package](https://www.npmjs.com/package/lodash.attempt)

**描述：**

尝试调用`func`，返回结果 或者 捕捉错误对象。任何附加的参数都会在调用时传给`func`。

**开始版本：**&#x33;.0.0

**参数：**

* `func (Function)`: 要尝试调用的函数。
* `[args] (...*)`: 调用func时，传递的参数。

**返回值：**

* `(*)`: 返回func结果或者错误对象。

**例子：**

```javascript
// Avoid throwing errors for invalid selectors.
var elements = _.attempt(function(selector) {
  return document.querySelectorAll(selector);
}, '>_>');

if (_.isError(elements)) {
  elements = [];
}
```
