<!--
{
  "availability" : [
    "iOS: 14.0.0 -",
    "iPadOS: 14.0.0 -",
    "macCatalyst: 14.0.0 -",
    "macOS: 11.0.0 -",
    "tvOS: 14.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 7.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "System",
  "identifier" : "/documentation/System/FileDescriptor/closeAfter(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "System"
    ],
    "preciseIdentifier" : "s:6System14FileDescriptorV10closeAfteryxxyKXEKlF"
  },
  "title" : "closeAfter(_:)"
}
-->

# closeAfter(_:)

Runs a closure and then closes the file descriptor, even if an error occurs.

```
func closeAfter<R>(_ body: () throws -> R) throws -> R
```

## Parameters

`body`

The closure to run.
If the closure throws an error,
this method closes the file descriptor before it rethrows that error.

## Return Value

The value returned by the closure.

## Discussion

If `body` throws an error
or an error occurs while closing the file descriptor,
this method rethrows that error.

---

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)