How to stop program execution at some point

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

How to stop program execution at some point

Fred
Hi,

I search in Julia the equivalent of Perl 
die;
to stop the execution of a program.

I tried "break" but it produces an error :
ERROR: LoadError: syntax: break or continue outside loop
I did not found "stop" as well.

Thanks !
Reply | Threaded
Open this post in threaded view
|

Re: How to stop program execution at some point

Michele Zaffalon
If I understand correctly and die raises an exception in Perl, then error() is the equivalent in Julia.
`break` can only be used inside a loop, for instance when a condition is met:

while true
  cond
&& break
end




On Monday, November 7, 2016 at 11:21:14 AM UTC+1, Fred wrote:
Hi,

I search in Julia the equivalent of Perl 
die;
to stop the execution of a program.

I tried "break" but it produces an error :
ERROR: LoadError: syntax: break or continue outside loop
I did not found "stop" as well.

Thanks !
Reply | Threaded
Open this post in threaded view
|

Re: How to stop program execution at some point

Fred
Yes Michele, "die" is able to stop execution at any point, even outside a loop, this is what I searched and did not found in Julia.
Reply | Threaded
Open this post in threaded view
|

Re: How to stop program execution at some point

Tamas Papp
In reply to this post by Fred
See quit(), and in general exit(code).

On Monday, November 7, 2016 at 11:21:14 AM UTC+1, Fred wrote:
Hi,

I search in Julia the equivalent of Perl 
die;
to stop the execution of a program.

I tried "break" but it produces an error :
ERROR: LoadError: syntax: break or continue outside loop
I did not found "stop" as well.

Thanks !
Reply | Threaded
Open this post in threaded view
|

Re: How to stop program execution at some point

Fred
Thank you Tamas, quit() is exactly what I searched ;)

Le lundi 7 novembre 2016 11:40:32 UTC+1, Tamas Papp a écrit :
See quit(), and in general exit(code).
Reply | Threaded
Open this post in threaded view
|

Re: How to stop program execution at some point

Davide
In reply to this post by Tamas Papp
Actually, when executing a function in Julia REPL, quit() and exit() also exit from julia. Is there a way to stop the function but not Julia?

   Thanks

On Monday, November 7, 2016 at 11:40:32 AM UTC+1, Tamas Papp wrote:
See quit(), and in general exit(code).
Reply | Threaded
Open this post in threaded view
|

Re: How to stop program execution at some point

Tamas Papp
return

On Fri, Nov 11 2016, Davide wrote:

> Actually, when executing a function in Julia REPL, quit() and exit() also
> exit from julia. Is there a way to stop the function but not Julia?
>
>    Thanks
>
> On Monday, November 7, 2016 at 11:40:32 AM UTC+1, Tamas Papp wrote:
>>
>> See quit(), and in general exit(code).
>>

Reply | Threaded
Open this post in threaded view
|

Re: How to stop program execution at some point

Angel de Vicente
Tamas Papp <[hidden email]> writes:

> return

But return will only quit the current function and return execution to
the caller. Coming from Fortran, I also miss something like STOP, which
will stop all execution and return me to the REPL.
--
Ángel de Vicente
http://www.iac.es/galeria/angelv/         
Reply | Threaded
Open this post in threaded view
|

Re: How to stop program execution at some point

Tamas Papp
On Fri, Nov 11 2016, Angel de Vicente wrote:

> Tamas Papp <[hidden email]> writes:
>
>> return
>
> But return will only quit the current function and return execution to
> the caller. Coming from Fortran, I also miss something like STOP, which
> will stop all execution and return me to the REPL.

I hear you --- I miss RETURN-FROM from Common Lisp.

Simply throwing an error will stop execution and return to the REPL
though.
Reply | Threaded
Open this post in threaded view
|

Re: How to stop program execution at some point

Angel de Vicente
Tamas Papp <[hidden email]> writes:
>> But return will only quit the current function and return execution to
>> the caller. Coming from Fortran, I also miss something like STOP, which
>> will stop all execution and return me to the REPL.
>
> I hear you --- I miss RETURN-FROM from Common Lisp.
>
> Simply throwing an error will stop execution and return to the REPL
> though.

Yes, I was using that, but it is not very clean. It gives the wrong
impression that an error happened (which in many cases it is probably
true, but not always) plus it prints the call stack at the point of the
error. If error() could be told to be silent, it would be almost
perfect. Anyway, a minor thing.

Cheers,
--
Ángel de Vicente
http://www.iac.es/galeria/angelv/         
Reply | Threaded
Open this post in threaded view
|

Re: How to stop program execution at some point

Tamas Papp

On Fri, Nov 11 2016, Angel de Vicente wrote:

> Tamas Papp <[hidden email]> writes:
>>> But return will only quit the current function and return execution to
>>> the caller. Coming from Fortran, I also miss something like STOP, which
>>> will stop all execution and return me to the REPL.
>>
>> I hear you --- I miss RETURN-FROM from Common Lisp.
>>
>> Simply throwing an error will stop execution and return to the REPL
>> though.
>
> Yes, I was using that, but it is not very clean. It gives the wrong
> impression that an error happened (which in many cases it is probably
> true, but not always) plus it prints the call stack at the point of the
> error. If error() could be told to be silent, it would be almost
> perfect. Anyway, a minor thing.

immutable Stop <: Exception end

function run_task(f)
    try f()
    catch e
        if isa(e, Stop)
            println("Your program was gracefully terminated.")
        else
            rethrow()
        end
    end
end

function f()
    throw(Stop())
end

function g()
    f()
end

julia>  run_task() do
            g()
        end
Your program was gracefully terminated.

julia>  run_task() do
            error("This is bad.")
        end
ERROR: This is bad.
 in run_task(::##84#85) at ./REPL[167]:2
Reply | Threaded
Open this post in threaded view
|

Re: How to stop program execution at some point

Angel de Vicente
Nice. Thanks!

>> Yes, I was using that, but it is not very clean. It gives the wrong
>> impression that an error happened (which in many cases it is probably
>> true, but not always) plus it prints the call stack at the point of the
>> error. If error() could be told to be silent, it would be almost
>> perfect. Anyway, a minor thing.
>
> immutable Stop <: Exception end
>
> function run_task(f)
>     try f()
>     catch e
>         if isa(e, Stop)
>             println("Your program was gracefully terminated.")
>         else
>             rethrow()
>         end
>     end
> end
>
> function f()
>     throw(Stop())
> end
>
> function g()
>     f()
> end
>
> julia>  run_task() do
>             g()
>         end
> Your program was gracefully terminated.
>
> julia>  run_task() do
>             error("This is bad.")
>         end
> ERROR: This is bad.
>  in run_task(::##84#85) at ./REPL[167]:2
>

--
Ángel de Vicente
http://www.iac.es/galeria/angelv/