# How to stop script execution

**URL:** <https://discourse.julialang.org/t/how-to-stop-script-execution/38568>\
**Category:** General Usage\
**Created:** [May 1, 2020, 5:39pm UTC](https://discourse.julialang.org/t/how-to-stop-script-execution/38568 "2020-05-01T17:39:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hmemy](https://avatars.discourse-cdn.com/v4/letter/h/50afbb/32.png) [@hmemy](https://discourse.julialang.org/u/hmemy)\
**Post date:** [May 1, 2020, 5:39pm UTC](https://discourse.julialang.org/t/how-to-stop-script-execution/38568/1 "2020-05-01T17:39:22Z")

</div>

Hi,

How can I stop script execution if certain condition is not met? `exit()` does not work here as it exit julia.

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [May 1, 2020, 6:11pm UTC](https://discourse.julialang.org/t/how-to-stop-script-execution/38568/2 "2020-05-01T18:11:24Z")

</div>

I usually avoid scripts and pack things into functions. In that way I can gracefully `return` if a condition is met (or not). You can also `break` to exit `for loops`, without exiting the function that contains those `for loops`.  
Hope this helps.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 1, 2020, 6:12pm UTC](https://discourse.julialang.org/t/how-to-stop-script-execution/38568/3 "2020-05-01T18:12:28Z")

</div>

What do you mean by script and how did you run it? Did you mean including a file?

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [May 1, 2020, 6:19pm UTC](https://discourse.julialang.org/t/how-to-stop-script-execution/38568/4 "2020-05-01T18:19:47Z")

</div>

This thread might be helpful to you. It sounds like you are trying to do something similar.

> [@A Julia equivalent to R's stop()?](https://discourse.julialang.org/t/a-julia-equivalent-to-rs-stop/36568/5):
>
> You could probably do something like: struct Stop{T} answer::T end function stop(answer) throw(Stop(answer)) end function start() stop(42) end try start() @info "No answer." catch ex if isa(ex, Stop) @info "Exit with: $(ex.answer)" else rethrow(ex) end end This assumes that you are not already catching exceptions in the stack. If you are then you probably want to throw a “Stop” object that the other exception handlers can rethrow. This is al…

---

<div class="post-metadata">

**Author:** ![hmemy](https://avatars.discourse-cdn.com/v4/letter/h/50afbb/32.png) [@hmemy](https://discourse.julialang.org/u/hmemy)\
**Post date:** [May 2, 2020, 6:56am UTC](https://discourse.julialang.org/t/how-to-stop-script-execution/38568/5 "2020-05-02T06:56:18Z")

</div>

Hi @yuyichao

Sometimes conditions cannot be met by the input provided by the user. In this case, there is no point of continuing running the code, and I would rather exit with a message. I am looking for something similar to `return` in MATLAB.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [May 2, 2020, 12:09pm UTC](https://discourse.julialang.org/t/how-to-stop-script-execution/38568/6 "2020-05-02T12:09:00Z")

</div>

Something like this?

```julia
function main()
    # code
    if break_condition
        return
    end
    # more code
end
main()

```

An other advantage is that this avoids global variables, which are bad for performance.

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [May 2, 2020, 12:33pm UTC](https://discourse.julialang.org/t/how-to-stop-script-execution/38568/7 "2020-05-02T12:33:40Z")

</div>

I asked that on stack overflow once and there isn’t really a satisfactory solution to it, besides just throwing an error:

[https://stackoverflow.com/questions/35226337/how-to-quit-exit-from-withing-included-file-file](https://stackoverflow.com/questions/35226337/how-to-quit-exit-from-withing-included-file-file)

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 2, 2020, 1:19pm UTC](https://discourse.julialang.org/t/how-to-stop-script-execution/38568/8 "2020-05-02T13:19:11Z")

</div>

That isn’t what I asked. The question is what you meant by “script execution”.

If you indeed mean by `include`ing a file, then no there’s no global control flow. You could throw an error to abort execution but you should just use a function instead.

---

<div class="post-metadata">

**Author:** ![texadactyl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/texadactyl/32/7007_2.png) [@texadactyl](https://discourse.julialang.org/u/texadactyl)\
**Post date:** [February 3, 2021, 6:29pm UTC](https://discourse.julialang.org/t/how-to-stop-script-execution/38568/9 "2021-02-03T18:29:41Z")

</div>

I think that I understand the question from @hmemy  
Java, Python, C/C++, Fortran, etc. provide a system function that will immediately exit to the resident operating system. Python: sys.exit(n). Java: System.exit(n). It doesn’t matter if the caller is the main program, a function, or an included module, whatever.

There are situations where this is useful. For example, a function (“oops”) is being called to tidy up (flush butters, close files etc.), println a clear application-specific message, and then exit to the O/S directly. Tracebacks serve no purpose in this case because the oops-function can fully communicate what the problem is without the distracting clutter of a traceback.

You can implement this yourself as exemplified:

```julia
function oops(msg::AbstractString)
    println("\n*** Oops, $msg\n")
    # close database, close files, .....
    ccall(:jl_exit, Cvoid, (Int32,), 86) # Exit code 86 to the O/S
end

```

---

<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:** [February 7, 2021, 11:49am UTC](https://discourse.julialang.org/t/how-to-stop-script-execution/38568/10 "2021-02-07T11:49:39Z")

</div>

> [@texadactyl](#):
>
> `ccall(:jl_exit, Cvoid, (Int32,), 86) # Exit code 86 to the O/S`

Or — at the risk of undercomplicating it — just `exit(86)`.
