# Is it possible to make early exit in do-try-catch block? (Like \`guard\`)

**URL:** <https://forums.swift.org/t/is-it-possible-to-make-early-exit-in-do-try-catch-block-like-guard/21551>\
**Category:** Discussion\
**Tags:** question\
**Created:** [March 13, 2019, 7:36am UTC](https://forums.swift.org/t/is-it-possible-to-make-early-exit-in-do-try-catch-block-like-guard/21551 "2019-03-13T07:36:22Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![rnantes](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rnantes/32/5653_2.png) [@rnantes](https://forums.swift.org/u/rnantes)\
**Post date:** [October 14, 2019, 4:50pm UTC](https://forums.swift.org/t/is-it-possible-to-make-early-exit-in-do-try-catch-block-like-guard/21551/10 "2019-10-14T16:50:44Z")

</div>

There have been some [pitches](https://forums.swift.org/t/guard-catch/6264) for this guard catch syntax and I think it would be extremely valuable to swift. For my use case I am using SwiftNio and as recommended I want to return failed futures instead of throwing errors. I want to do something like:

```nohighlight
guard let encodedJson = try JSONEncoder().encode(myCodableObject) catch {
    // return failed eventLoopFuture containing error
    return group.next().makeFailedFuture(error)
}
...

```

Unfortunately without `guard/try/catch` my code is much more verbose

```nohighlight
var encodedJSONOptional: Data? = nil
do {
    encodedJSONOptional = try JSONEncoder().encode(myCodableObject)
} catch {
     return group.next().makeFailedFuture(error)
 }
guard let encodedJSON = encodedJSONOptional else {
    return group.next().makeFailedFuture(SomeError.couldNotEncodeAsJSON)
}
...

```

---

_[View the full topic](https://forums.swift.org/t/is-it-possible-to-make-early-exit-in-do-try-catch-block-like-guard/21551)._
