# Error when file is not found

**URL:** https://discourse.julialang.org/t/error-when-file-is-not-found/106665
**Category:** General Usage
**Tags:** filesystem
**Created:** [November 24, 2023, 7:46am UTC](https://discourse.julialang.org/t/error-when-file-is-not-found/106665 "2023-11-24T07:46:06Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Barvid](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Barvid](https://discourse.julialang.org/u/Barvid)
#### Post date: [November 24, 2023, 7:46am UTC](https://discourse.julialang.org/t/error-when-file-is-not-found/106665/1 "2023-11-24T07:46:06Z")

</div>

Hello All

I am sure there is super simple elegant solution to this, but I cant find it, so sorry for the silly question.

my code loads a csv file and sometimes the file is not there, so need to handle that. I found 2 ways, neither is elegant.

a) return from the function with some error message and then I have to handle that error message in all the 5 function calls that are between the repl and me loading the file. I will not do that unnecessary coding.

b) what I do now which is to try to load the file which of course fails, and gives me hundreds of lines of error messages. not only is this extremely inefficient but also unelegant,

this is super easy in python and R (in R call stop(“file not found”))

so, since my search abilities failed me, could anybody please tell me how I can simply stop the execution of a script in the repl when my file is not there?

thanks,

s

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [November 24, 2023, 8:39am UTC](https://discourse.julialang.org/t/error-when-file-is-not-found/106665/2 "2023-11-24T08:39:36Z")

</div>

You can load the file in a `try`/`catch` block, where you can catch the case where the file doesn’t exist. When that happens, you throw a nice error message.

To avoid showing a large stacktrace you can add another `try`/`catch` block at the root level of your script. You could do just that, without the `try`/`catch` block described in the previous paragraph, but then it can be harder to know what error you’re dealing with at the root level (when you have a `try` close to the source of the error, you can throw for example a custom error type that’s easy to identify at the root level).

To reproduce the R behavior see [A Julia equivalent to R's stop()? - #13 by kristoffer.carlsson](https://discourse.julialang.org/t/a-julia-equivalent-to-rs-stop/36568/13). It’s not recommended because it doesn’t compose well: maybe your nice error message is enough in your current use case. But if your code ends up being used as a piece in a larger program, then the error message without context might make it more difficult to understand and solve.

---

<div class="post-metadata">

### Author: ![Barvid](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Barvid](https://discourse.julialang.org/u/Barvid)
#### Post date: [November 24, 2023, 10:41am UTC](https://discourse.julialang.org/t/error-when-file-is-not-found/106665/3 "2023-11-24T10:41:42Z")

</div>

Thanks @sijo

Yes, try can be useful, just that I want to end the run when the error happens. I usually use `exit()` as my code runs in terminal usually, but when editing in the repl don’t want to reload everything.

I had not seen that post by Carlsson, looks like it solves the problem, I should have found it. It will now have a honord place in my startup script.

I do not understand why it is a bad idea, maybe when doing something really sophisticated, but thats not me. I only know how to do simple stuff and am happy with a nice error message and code stopping.

thanks!!  
s

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [November 24, 2023, 12:25pm UTC](https://discourse.julialang.org/t/error-when-file-is-not-found/106665/4 "2023-11-24T12:25:04Z")

</div>

> [@Barvid](#):
>
> Yes, try can be useful, just that I want to end the run when the error happens.

You can still do that with `try` though, for example make a script like this:

```julia
function do_stuff()
    # do lots of stuff here
    ...
end

try
    result = to_stuff()
catch
    println("Some error with explanation")
end

```

---

<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: [November 24, 2023, 1:14pm UTC](https://discourse.julialang.org/t/error-when-file-is-not-found/106665/5 "2023-11-24T13:14:24Z")

</div>

Check out: `isfile()`

---

<div class="post-metadata">

### Author: ![Barvid](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Barvid](https://discourse.julialang.org/u/Barvid)
#### Post date: [November 24, 2023, 2:32pm UTC](https://discourse.julialang.org/t/error-when-file-is-not-found/106665/6 "2023-11-24T14:32:59Z")

</div>

thanks @sijo

Unless I am missing something obvious, try-catch does not terminate execution, it only allows one to continue after errors, which in my case I don’t want to do as there is no file with input data, so nothing to run. In the catch would be great to do a R style `stop()`.

best, s

---

<div class="post-metadata">

### Author: ![Barvid](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Barvid](https://discourse.julialang.org/u/Barvid)
#### Post date: [November 24, 2023, 2:35pm UTC](https://discourse.julialang.org/t/error-when-file-is-not-found/106665/7 "2023-11-24T14:35:02Z")

</div>

thanks @rafael.guerra

I am indeed using `isfile()`, serves here same purpose as `try-catch`, but I need to terminate execution if `isfile()` is `false`. Hence need for R style `stop()`.

best s

---

<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: [November 24, 2023, 2:37pm UTC](https://discourse.julialang.org/t/error-when-file-is-not-found/106665/8 "2023-11-24T14:37:35Z")

</div>

Could your function return nothing or an error if no file exists?

`!isfile("...") && (return nothing)`

---

<div class="post-metadata">

### Author: ![Barvid](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Barvid](https://discourse.julialang.org/u/Barvid)
#### Post date: [November 24, 2023, 2:43pm UTC](https://discourse.julialang.org/t/error-when-file-is-not-found/106665/9 "2023-11-24T14:43:25Z")

</div>

Hi @rafael.guerra

indeed, but it gets a bit tedious to propagate that up 5 function calls to the top call which will end up terminating the run anyways. Seems much more efficient and less error prone to terminate immediately.

best s

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [November 24, 2023, 2:55pm UTC](https://discourse.julialang.org/t/error-when-file-is-not-found/106665/10 "2023-11-24T14:55:57Z")

</div>

> [@Barvid](#):
>
> Unless I am missing something obvious, try-catch does not terminate execution,

In my previous message, the idea is that you put the whole “work” inside of a function (called `do_stuff` in my example). Then outside you just have the `try` block, this way there is nothing to stop when you are in `catch`. It’s equivalent to putting the whole script in a try/catch, but a bit nicer.

---

<div class="post-metadata">

### Author: ![Barvid](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Barvid](https://discourse.julialang.org/u/Barvid)
#### Post date: [November 24, 2023, 3:02pm UTC](https://discourse.julialang.org/t/error-when-file-is-not-found/106665/11 "2023-11-24T15:02:14Z")

</div>

Hi @sijo

ah I see. would indeed work. downside is that all errors get hidden along with it, and one usually wants to see them all

best,  
s

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [November 24, 2023, 3:23pm UTC](https://discourse.julialang.org/t/error-when-file-is-not-found/106665/12 "2023-11-24T15:23:24Z")

</div>

You can simply `rethrow` the error if it’s not the one you want to process in the try-catch block. Example:

```julia
function do_stuff()
    ...
    ...
    try
        # operation on file
    catch
        throw(MyFileError(...))
    end
    ...
    ...
end

# End of script
try
    do_stuff()
catch e
    if e isa MyFileError
        println("Blabla $e")
    else
        rethrow()
    end
end

```

---

<div class="post-metadata">

### Author: ![Barvid](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Barvid](https://discourse.julialang.org/u/Barvid)
#### Post date: [November 24, 2023, 8:21pm UTC](https://discourse.julialang.org/t/error-when-file-is-not-found/106665/13 "2023-11-24T20:21:30Z")

</div>

@sijo

fantastic, thanks

s
