---
title: "require(throws:_:sourceLocation:performing:)"
framework: testing
role: symbol
role_heading: Macro
path: "testing/require(throws:_:sourcelocation:performing:)-7n34r"
---

# require(throws:_:sourceLocation:performing:)

Check that an expression always throws an error of a given type, and throw an error if it does not.

## Declaration

```swift
@discardableResult @freestanding(expression) macro require<E, R>(throws errorType: E.Type, _ comment: @autoclosure () -> Comment? = nil, sourceLocation: SourceLocation = #_sourceLocation, performing expression: () async throws -> R) -> E where E : Error
```

## Parameters

- `errorType`: The type of error that is expected to be thrown. If expression could throw any error, or the specific type of thrown error is unimportant, pass (any Error).self.
- `comment`: A comment describing the expectation.
- `sourceLocation`: The source location to which recorded expectations and issues should be attributed.
- `expression`: The expression to be evaluated.

## Mentioned in

Testing for errors in Swift code

## Return Value

Return Value The instance of errorType that was thrown by expression.

## Overview

Overview note: An instance of ExpectationFailedError if expression does not throw a matching error. The error thrown by expression is not rethrown. Use this overload of #require() when the expression expression should throw an error of a given type: try #require(throws: EngineFailureError.self) {   FoodTruck.shared.engine.batteryLevel = 0   try FoodTruck.shared.engine.start() } If expression does not throw an error, or if it throws an error that is not an instance of errorType, an Issue is recorded for the test that is running in the current task and an instance of ExpectationFailedError is thrown. Any value returned by expression is discarded. note: If you use this macro with a Swift compiler version lower than 6.1, it doesn’t return a value. If the thrown error need only equal another instance of Error, use require(throws:_:sourceLocation:performing:) instead. If expression should never throw, simply invoke the code without using this macro. The test will then fail if an error is thrown.

## See Also

### Checking that errors are thrown

- [Testing for errors in Swift code](testing/testing-for-errors-in-swift-code.md)
- [expect(throws:_:sourceLocation:performing:)](testing/expect(throws:_:sourcelocation:performing:)-1hfms.md)
- [expect(throws:_:sourceLocation:performing:)](testing/expect(throws:_:sourcelocation:performing:)-7du1h.md)
- [expect(_:sourceLocation:performing:throws:)](testing/expect(_:sourcelocation:performing:throws:).md)
- [require(throws:_:sourceLocation:performing:)](testing/require(throws:_:sourcelocation:performing:)-4djuw.md)
- [require(_:sourceLocation:performing:throws:)](testing/require(_:sourcelocation:performing:throws:).md)
