<!--
{
  "availability" : [

  ],
  "documentType" : "symbol",
  "framework" : "XCTest",
  "identifier" : "/documentation/XCTest/XCTAssertThrowsError(_:_:file:line:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "XCTest"
    ],
    "preciseIdentifier" : "s:6XCTest20XCTAssertThrowsError__4file4line_yxyKXK_SSyXKs12StaticStringVSuys0D0_pXEtlF"
  },
  "title" : "XCTAssertThrowsError(_:_:file:line:_:)"
}
-->

# XCTAssertThrowsError(_:_:file:line:_:)

Asserts that an expression throws an error.

```
func XCTAssertThrowsError<T>(_ expression: @autoclosure () throws -> T, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line, _ errorHandler: (any Error) -> Void = { _ in })
```

## Parameters

`expression`

An expression that can throw an error.

`message`

An optional description of a failure.

`file`

The file where the failure occurs. The default is the filename of the test case where you call this function.

`line`

The line number where the failure occurs. The default is the line number where you call this function.

`errorHandler`

An optional handler for errors that `expression` throws.

## Discussion

---

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)