Untyped throws (UntypedThrows)
A Swift function can declare that it can throw an error using throws, optionally providing a specific thrown error type:
Overview
func canThrowAnything() throws { ... } // untyped throws
func canThrowMyError() throws(MyError) { ... } // typed throws (only throws MyError)When a specific thrown error type is omitted, any Error will be used instead. Throwing an error of type any Error involves a heap allocation as well as reference-counting overhead. Therefore, highly performance-sensitive code should prefer to use typed throws over untyped throws.
See Also
Related Documentation
@dynamicCallable implementation requirements (DynamicCallable)Add @preconcurrency import (AddPreconcurrencyImport)Always enabled availability domains (AlwaysAvailableDomain)Argument matching for trailing closures (TrailingClosureMatching)Calling a mutating async actor-isolated method (ActorIsolatedMutatingAsync)Calling an actor-isolated method from a synchronous nonisolated context (ActorIsolatedCall)Captures in a `@Sendable` closure (SendableClosureCaptures)Compilation caching (CompilationCaching)Conforming to `StringInterpolationProtocol` (StringInterpolationConformance)Conversion from `@isolated(any)` function type to synchronous function type (ConversionFromIsolatedAnyToSynchronous)Cross-isolation data race (RegionIsolationCrossIsolationDataRace)Deprecated declaration warnings (DeprecatedDeclaration)Deprecated implementation-only imports (ImplementationOnlyDeprecated)Dynamic exclusivity (DynamicExclusivity)Embedded Swift language restrictions (EmbeddedRestrictions)