<!--
{
  "availability" : [
    "Xcode: 16.0.0 -",
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "macOS: -",
    "swift: 6.0.0 -",
    "tvOS: -",
    "visionOS: -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "Testing",
  "identifier" : "/documentation/Testing/require(throws:_:sourceLocation:performing:)-4djuw",
  "metadataVersion" : "0.1.0",
  "role" : "Macro",
  "symbol" : {
    "kind" : "Macro",
    "modules" : [
      "Swift Testing"
    ],
    "preciseIdentifier" : "s:7Testing7require6throws_14sourceLocation10performingxx_AA7CommentVSgyXKAA06SourceE0Vq_yYaKXEtcSQRzs5ErrorRzr0_lufm"
  },
  "title" : "require(throws:_:sourceLocation:performing:)"
}
-->

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

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

## Return Value

The instance of `E` that was thrown by `expression` and is equal
to `error`.

## Overview> Throws: An instance of ``doc://org.swift.testing/documentation/Testing/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 a specific error:

```swift
try #require(throws: EngineFailureError.batteryDied) {
  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 equal to `error`, an [`Issue`](/documentation/Testing/Issue) is recorded for the test that is running
in the current task and an instance of [`ExpectationFailedError`](/documentation/Testing/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 be an instance of a particular type, use
[`require(throws:_:sourceLocation:performing:)`](/documentation/Testing/require(throws:_:sourceLocation:performing:)-7n34r) instead.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)