Error when file is not found

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

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. 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.

2 Likes

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

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

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

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

Check out: isfile()

1 Like

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

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

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

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

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

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.

1 Like

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

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

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
1 Like

@sijo

fantastic, thanks

s

1 Like