What is the equivalent of " return 0; " used in C in Julia during successful completion of program ?

754 views
Skip to first unread message

Rishabh Raghunath

unread,
Aug 15, 2016, 1:40:15 AM8/15/16
to julia-users
Hello Julia Users..
I'd like to know What the equivalent of "return 0  used in C"  is in Julia while completion of program ..
And..
I have an issue with juno ide.. Juno is not getting input using readline(STDIN) .. Its not prompting me for input.. It used to work before .. Although,. the same code works perfectly fine while I try to run it via the terminal..



Tamas Papp

unread,
Aug 15, 2016, 2:24:02 AM8/15/16
to julia...@googlegroups.com
You can use exit(0), but the exit code is implicitly 0 anyway when your
Julia script runs without errors. See
http://docs.julialang.org/en/release-0.4/stdlib/base/

Rishabh Raghunath

unread,
Aug 16, 2016, 1:58:57 AM8/16/16
to julia-users
Got it !!.. Thanks a lot !!

Páll Haraldsson

unread,
Aug 18, 2016, 6:42:58 AM8/18/16
to julia-users
On Monday, August 15, 2016 at 6:24:02 AM UTC, Tamas Papp wrote:
You can use exit(0), but the exit code is implicitly 0 anyway when your
Julia script runs without errors. See
http://docs.julialang.org/en/release-0.4/stdlib/base/

Yes, I did check, the default is 1 on exception/error (there are no known errors to me except subtypes of Exception).

julia -e "type MyCustomException <: Exception end; throw(MyCustomException)"; echo $?
ERROR: MyCustomException
1

julia -e "throw(UnicodeError)"; echo $?
ERROR: MyCustomException
1


When is there a need to call exit(n) # with non-zero n? Or n>1; would throwing an exception always be better than at least exit(1)?


julia> @edit throw(MyCustomException)
ERROR: ArgumentError: argument is not a generic function
 in methods at ./reflection.jl:140

I can't see that anything other than 1 is ever thrown on exception [as throw is a keyword and not really a function, so I'm not sure where to check..]


Is there a need (or a possibility, seems not, only allows for a message (in only one language..)) to throw exceptions with some other non-zero error code? [I guess it's bad form to catch exceptions and then call exit.. or call exit from atexit handler..]


This is what I found:

exit(n) = ccall(:jl_exit, Void, (Int32,), n)
exit() = exit(0)
quit() = exit()


So if at the end of your script exit() is called you get 0 exit code for sure.


bash manual:

"An OR list has the form

              command1 || command2

       command2 is executed if and only if command1 returns a non-zero exit status

[..]

?      Expands to the exit status of the most recently executed foreground pipeline.

[..]

PIPESTATUS
              An array variable (see Arrays below) containing a list of exit status values from the processes in the most-recently-executed foreground pipeline (which may contain only a single  com‐
              mand).

[..]

If the name is neither a shell function nor a builtin, and contains no slashes, bash searches each element of the PATH for a directory containing an executable file by that name.  Bash uses a
       hash table to remember the full pathnames of executable files (see hash under SHELL BUILTIN COMMANDS below).  A full search of the directories in PATH is performed only if the command is  not
       found in the hash table.  If the search is unsuccessful, the shell searches for a defined shell function named command_not_found_handle.  If that function exists, it is invoked with the orig‐
       inal command and the original command's arguments as its arguments, and the function's exit status becomes the exit status of the shell.  If that function is not defined, the shell prints  an
       error message and returns an exit status of 127.

[..]

EXIT STATUS
       The exit status of an executed command is the value returned by the waitpid system call or equivalent function.  Exit statuses fall between 0 and 255, though, as explained  below,  the  shell
       may  use values above 125 specially.  Exit statuses from shell builtins and compound commands are also limited to this range. Under certain circumstances, the shell will use special values to
       indicate specific failure modes.

       For the shell's purposes, a command which exits with a zero exit status has succeeded.  An exit status of zero indicates success.  A non-zero exit status indicates failure.   When  a  command
       terminates on a fatal signal N, bash uses the value of 128+N as the exit status.

       If a command is not found, the child process created to execute it returns a status of 127.  If a command is found but is not executable, the return status is 126.

       If a command fails because of an error during expansion or redirection, the exit status is greater than zero.

       Shell  builtin  commands  return a status of 0 (true) if successful, and non-zero (false) if an error occurs while they execute.  All builtins return an exit status of 2 to indicate incorrect
       usage.

       Bash itself returns the exit status of the last command executed, unless a syntax error occurs, in which case it exits with a non-zero value.  See also the exit builtin command below."

--
Palli.

Tamas Papp

unread,
Aug 18, 2016, 7:06:36 AM8/18/16
to julia...@googlegroups.com
While I am not sure I understand your question, the way you handle
exceptions and exit status depends on what you want to do; and this is
mostly independent of whether you are using Julia or some other
language.

In a mature script intended for non-expert users, it would be bad form
to show messages about uncaught exceptions, so you should catch them,
display an error message, and follow some conventions about exit status
that you document in the man page. (Given that Julia is a relatively new
language, you don't see many of these scritps in the wild (yet)).

In a quick & dirty script intended for your own use, you basically do
what you like; for convenience I don't catch exceptions in my own Julia
scripts and just have them display error messages like the ones you show
below. This is sufficient for integration into tools like make (eg for
data analysis where each script handles a single step).

NB: 1. you can't call @edit on throw, it is a built-in, not a generic.
2. the fact that exit() defaults to 0 is documented, you don't need to
look at the source.
Reply all
Reply to author
Forward
0 new messages