# What is an idiomatic Julia code for file I/O with exceptions handled?

**URL:** https://discourse.julialang.org/t/what-is-an-idiomatic-julia-code-for-file-i-o-with-exceptions-handled/3361
**Category:** General Usage
**Tags:** question, io
**Created:** [April 24, 2017, 10:47pm UTC](https://discourse.julialang.org/t/what-is-an-idiomatic-julia-code-for-file-i-o-with-exceptions-handled/3361 "2017-04-24T22:47:11Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)
#### Post date: [April 24, 2017, 10:47pm UTC](https://discourse.julialang.org/t/what-is-an-idiomatic-julia-code-for-file-i-o-with-exceptions-handled/3361/1 "2017-04-24T22:47:12Z")

</div>

What is an idiomatic Julia code for `tryOpen` function below, which tests if 2 files can be opened and handles all exceptions? I’m looking for code replacing the comments or totally different code structure, if needed.

```julia
function tryOpen(fileName1, fileName2)
    try
        f1 = open(fileName1)
        f2 = open(fileName2)
        println("$fileName1 and $fileName2 can be opened")
    catch
        #print open file names
    finally
        #close opened files
    end
end

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [April 24, 2017, 11:17pm UTC](https://discourse.julialang.org/t/what-is-an-idiomatic-julia-code-for-file-i-o-with-exceptions-handled/3361/2 "2017-04-24T23:17:35Z")

</div>

> [@pauljurczak](#):
>
> open(fileName1)

If you want to just open and then (if successful) immediately close the file, you can do `open(f->nothing, filename)`. This is equivalent to

```julia
open(filename) do f
    nothing
end

```

which ensures that the file is closed immediately (as opposed to waiting until garbage-collection happens). Or you can pass any other do-nothing function for the first argument, e.g. `open(identity, filename)`

Putting it together, you can do:

```julia
function isopenable(filenames)
    try
        open(identity, filename)
        return true
    catch
        return false
    end
end

```

To try multiple filenames, I would normally suggest just `isopenable(filename1) && isopenable(filename2)`, though of course you could define

```julia
isopenable(filenames...) = all(isopenable, filenames)

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [April 24, 2017, 11:30pm UTC](https://discourse.julialang.org/t/what-is-an-idiomatic-julia-code-for-file-i-o-with-exceptions-handled/3361/3 "2017-04-24T23:30:15Z")

</div>

(Note that there is a built-in function `isfile` if you just want to check whether a file exists, or alternatively `isfile(filename) || islink(filename)`, and you can check `uperm(filename)` for read permissions.)

---

<div class="post-metadata">

### Author: ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)
#### Post date: [April 24, 2017, 11:33pm UTC](https://discourse.julialang.org/t/what-is-an-idiomatic-julia-code-for-file-i-o-with-exceptions-handled/3361/4 "2017-04-24T23:33:44Z")

</div>

> [@stevengj](#):
>
> open(f-\>nothing, filename)

This code will throw uncatched exception when file doesn’t exist.

My question is about acquiring/opening a group of resources, which may throw an exception. I would like to know in `catch` and `finally` blocks, which ones were safely opened. This can be done with a bunch of extra status variables, one per resource, but I wonder if there is a way to avoid them. File opening is just an example of acquiring a resource.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [April 24, 2017, 11:41pm UTC](https://discourse.julialang.org/t/what-is-an-idiomatic-julia-code-for-file-i-o-with-exceptions-handled/3361/5 "2017-04-24T23:41:23Z")

</div>

> [@pauljurczak](#):
>
> This code will throw uncatched exception when file doesn’t exist.

That’s why I put it in a `try` block.

> [@pauljurczak](#):
>
> My question is about acquiring/opening a group of resources, which may throw an exception.

Normally when you acquire a resource, you will associate some kind of handle object with it, e.g. an `IOStream` object for opening a file. You could always initialize these handles to some sentinel value, e.g. `IOStream("")`. In the `catch` block, you will know if they were successfully opened if they are different from the sentinel value.

---

<div class="post-metadata">

### Author: ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)
#### Post date: [April 24, 2017, 11:46pm UTC](https://discourse.julialang.org/t/what-is-an-idiomatic-julia-code-for-file-i-o-with-exceptions-handled/3361/6 "2017-04-24T23:46:55Z")

</div>

> [@stevengj](#):
>
> IOStream(“”)

Thanks, that’s what I was looking for. Beginner’s problem.

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [April 25, 2017, 5:46pm UTC](https://discourse.julialang.org/t/what-is-an-idiomatic-julia-code-for-file-i-o-with-exceptions-handled/3361/7 "2017-04-25T17:46:55Z")

</div>

To avoid confusing the code reader (and allocating an unnecessary object), I recommending using a sentinel singleton value like `nothing`.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [May 31, 2021, 8:26am UTC](https://discourse.julialang.org/t/what-is-an-idiomatic-julia-code-for-file-i-o-with-exceptions-handled/3361/8 "2021-05-31T08:26:08Z")

</div>

Where in Julia"s official docs may the answer to OP"s essential question be found?  
Thank you.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [May 31, 2021, 9:33am UTC](https://discourse.julialang.org/t/what-is-an-idiomatic-julia-code-for-file-i-o-with-exceptions-handled/3361/9 "2021-05-31T09:33:00Z")

</div>

[https://docs.julialang.org/en/v1/manual/control-flow/#The-try/catch-statement](https://docs.julialang.org/en/v1/manual/control-flow/#The-try/catch-statement)

But please consider not reviving ancient topics without a good reason.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [May 31, 2021, 9:51am UTC](https://discourse.julialang.org/t/what-is-an-idiomatic-julia-code-for-file-i-o-with-exceptions-handled/3361/10 "2021-05-31T09:51:52Z")

</div>

Sorry, but cannot even find half of the content posted above.
