# Extraneous end causes silent failure

**URL:** <https://discourse.julialang.org/t/extraneous-end-causes-silent-failure/18075>\
**Category:** General Usage\
**Created:** [November 27, 2018, 7:44pm UTC](https://discourse.julialang.org/t/extraneous-end-causes-silent-failure/18075 "2018-11-27T19:44:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [November 27, 2018, 7:44pm UTC](https://discourse.julialang.org/t/extraneous-end-causes-silent-failure/18075/1 "2018-11-27T19:44:54Z")

</div>

I had a very odd problem in that my program stopped running, and by stopped running I mean that the very first println in the function did not execute. However poking around in the code revealed that it was only one function that was not executing.

After some amount of cut-paste i finally tracked the problem down to:

```julia
function main()
...
for i=1:n 
  ...
  end
end
...
end

main()

```

An extraneous end. I figured that was the problem but it took me a bit to track down.

Apparently the extra end causes everything after it to be ignored (?), but there is no compilation error. It seems like the compiler should report an “unmatched end”.

Is this worthy of a bug report ?

---

<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:** [November 27, 2018, 7:52pm UTC](https://discourse.julialang.org/t/extraneous-end-causes-silent-failure/18075/2 "2018-11-27T19:52:00Z")

</div>

I can’t reproduce, please make an MWE that shows the end being ignored. Eg

```julia
shell> cat /tmp/test.jl
function main(n)
    for i=1:n
        println(i)
    end
end
end

main(10)

julia> include("/tmp/test.jl")
ERROR: LoadError: syntax: unexpected "end"
Stacktrace:
 [1] include at ./boot.jl:317 [inlined]
 [2] include_relative(::Module, ::String) at ./loading.jl:1038
 [3] include(::Module, ::String) at ./sysimg.jl:29
 [4] include(::String) at ./client.jl:396
 [5] top-level scope at none:0
in expression starting at /tmp/test.jl:6

julia> VERSION
v"1.1.0-DEV.720"

```

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [November 27, 2018, 7:57pm UTC](https://discourse.julialang.org/t/extraneous-end-causes-silent-failure/18075/3 "2018-11-27T19:57:57Z")

</div>

well that’s very odd. ok. I’ll work on the example. there’s quite a bit of code, so it will take me a bit to cut it down. The extraneous end is definitely causing the problem. it’s happened to me a couple of times.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [November 27, 2018, 7:59pm UTC](https://discourse.julialang.org/t/extraneous-end-causes-silent-failure/18075/4 "2018-11-27T19:59:06Z")

</div>

Where and how did you execute the code? Paste into the REPL, include the file in the REPL?

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [November 27, 2018, 8:07pm UTC](https://discourse.julialang.org/t/extraneous-end-causes-silent-failure/18075/5 "2018-11-27T20:07:21Z")

</div>

Well, this is fascinating. Simply uncomment the exit() to see the behavior. With exit commented, i get the compiler error. With exit() in the code, nothing happens, i.e. no compiler error and “test” is not printed.

I’m executing the file via

julia test.jl

and I’m using v"1.0.0"

```julia
function main()
    println("test")

    for i=1:5
        println("i=",i)
        end
    end
    #exit()
end

main()

```

edit:  
seems like what’s happening is  
1 the next-to-last end closes the function.  
2 exit() becomes a valid statement  
3 the last end is ignored  
4 the code runs and the exit() executes before main.

---

<div class="post-metadata">

**Author:** ![evanfields](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanfields/32/1744_2.png) [@evanfields](https://discourse.julialang.org/u/evanfields)\
**Post date:** [November 27, 2018, 8:14pm UTC](https://discourse.julialang.org/t/extraneous-end-causes-silent-failure/18075/6 "2018-11-27T20:14:56Z")

</div>

I think it’s just your indentation hiding what’s going on. I’d indent as

```julia
function main()
    println("test")

    for i=1:5
        println("i=",i)
    end
end
#exit()
end

```

So if the `exit()` is not commented out, then the script first defines function `main` and then exits. Note you can write anything on an unreachable line after an exit:

```julia
println("beginning")
exit()
hahaha this isn't even valid Julia code!

```

will run just fine, printing `beginning` before exiting.

If the `exit()` is _not_ commented out, then your script defines function `main` and then has an extraneous `end`, which is of course an error.

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [November 27, 2018, 9:37pm UTC](https://discourse.julialang.org/t/extraneous-end-causes-silent-failure/18075/7 "2018-11-27T21:37:39Z")

</div>

oh good grief that’s embarrassing…  
edit: ok. it’s still embarrassing, except, are you saying that the “exit()” is recognized by the compiler in some way ? in your example the compiler should absolutely be complaining about the last line. you seem to be saying that it sees the exit() and stops compiling which is not at all expected.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 27, 2018, 10:01pm UTC](https://discourse.julialang.org/t/extraneous-end-causes-silent-failure/18075/8 "2018-11-27T22:01:35Z")

</div>

The code is parsed and executed expression by expression.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 27, 2018, 10:04pm UTC](https://discourse.julialang.org/t/extraneous-end-causes-silent-failure/18075/9 "2018-11-27T22:04:12Z")

</div>

I don’t think it’s embarrassing at all—it makes sense but it’s still kind of surprising. I’m not sure if this is a bug or a feature…
