[Pitch N+1] Typed Throws

I do wonder if something like an attribute that can tell the compiler what errors a particular piece of API expects clients to handle could meet the needs of the “so you know you handled everything” crowd wanting typed throws.

Something like

@requiresHandlerFor(CatError)
func callCat() throws -> Cat { ... }
@requiresHandlerFor(KidError)
func callKids() throws -> [Kid] { ... }

do {
  try callCat()
  try callKids()
} catch let error as CatError {
 //handle the cat error
} //Compiler complains that KidError was expected to be handled and wasn’t

Would something like that meet the needs of the folks in that camp? Obviously this doesn’t help with the EmbeddedSwift motivation, but perhaps something like this (which I imagine would be relatively simply implemented, and could also probably be generated by an attached macro on the API types) might be a nice additional option that’s somewhere between the Wild West world of any Error and the strict lockdown world of typed throws.

2 Likes