# \_.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 = [];
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lodash.shujuwajue.com/util/attempt.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
