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

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

Check that an expression always throws a specific error.

## Declaration

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

## Parameters

- `error`: The error that is expected to be thrown.
- `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 If the expectation passes, the instance of E that was thrown by expression and is equal to error. If the expectation fails, the result is nil.

## Overview

Overview Use this overload of #expect() when the expression expression should throw a specific error: #expect(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 is recorded for the test that is running in the current task. 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 expect(throws:_:sourceLocation:performing:) instead.

## 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(_:sourceLocation:performing:throws:)](testing/expect(_:sourcelocation:performing:throws:).md)
- [require(throws:_:sourceLocation:performing:)](testing/require(throws:_:sourcelocation:performing:)-7n34r.md)
- [require(throws:_:sourceLocation:performing:)](testing/require(throws:_:sourcelocation:performing:)-4djuw.md)
- [require(_:sourceLocation:performing:throws:)](testing/require(_:sourcelocation:performing:throws:).md)
