# Curious about try?NSData(contentsOFURL)

**URL:** <https://forums.kodeco.com/t/curious-about-try-nsdata-contentsofurl/33498>\
**Category:** iOS Apprentice\
**Created:** [August 21, 2017, 2:21am UTC](https://forums.kodeco.com/t/curious-about-try-nsdata-contentsofurl/33498 "2017-08-21T02:21:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiang](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jiang/32/79544_2.png) [@jiang](https://forums.kodeco.com/u/jiang)\
**Post date:** [August 21, 2017, 2:21am UTC](https://forums.kodeco.com/t/curious-about-try-nsdata-contentsofurl/33498/1 "2017-08-21T02:21:06Z")

</div>

Hi fellows,

I’m curious about what  
try?NSData(contentsOFURL)

would return if the file doesn’t exist. I didn’t see the tutorial checks if the file exists, however, my app gets crushed without checking the existence of the file. I could not find details of this problem, I hope anyone knows and explains a bit to me.

Thanks!

---

<div class="post-metadata">

**Author:** ![hollance](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/hollance/32/109009_2.png) [@hollance](https://forums.kodeco.com/u/hollance)\
**Post date:** [August 21, 2017, 8:04am UTC](https://forums.kodeco.com/t/curious-about-try-nsdata-contentsofurl/33498/2 "2017-08-21T08:04:32Z")

</div>

That should return nil. Normally Data throws an error if the file cannot be found, but because you’re writing `try?` this gets turned into an optional.

---

<div class="post-metadata">

**Author:** ![jiang](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jiang/32/79544_2.png) [@jiang](https://forums.kodeco.com/u/jiang)\
**Post date:** [September 12, 2017, 8:35am UTC](https://forums.kodeco.com/t/curious-about-try-nsdata-contentsofurl/33498/3 "2017-09-12T08:35:56Z")

</div>

Thanks, I thought there would be something like throw

---

<div class="post-metadata">

**Author:** ![narrativium](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/narrativium/32/110937_2.png) [@narrativium](https://forums.kodeco.com/u/narrativium)\
**Post date:** [September 12, 2017, 9:12am UTC](https://forums.kodeco.com/t/curious-about-try-nsdata-contentsofurl/33498/4 "2017-09-12T09:12:56Z")

</div>

There is, but you specified `try?` - the `?` is important.

If you use `try` then you’ll need a `do { try ... } catch { ... }` statement to catch the error and respond accordingly.  
If you use `try?` then the function’s return type will be treated as an optional and will return nil if there’s an error.  
It’s a bit more complicated than that - if the function normally returns an optional (e.g an `Int?`), that optional will be wrapped in a second optional by the `try?` (e.g. it’ll now return an `Int??`). If the function returns `nil` legitimately, without throwing an error, you’ll still need to unwrap the value to find out if it was nil.

For the sake of completeness, there’s also `try!`, which if you use it and an error occurs, you’ll have a runtime error and your code will crash.

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [February 3, 2018, 6:21pm UTC](https://forums.kodeco.com/t/curious-about-try-nsdata-contentsofurl/33498/5 "2018-02-03T18:21:11Z")

</div>

Closed on February 3, 2018, 6:21pm UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
