Calling sys.exit with a string will work. The docs mention this use explicitly:
In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.
To be more specific this will also result in an exit code of 1:
Answer from lvc on Stack Overflowany other object is printed to stderr and results in an exit code of 1
Calling sys.exit with a string will work. The docs mention this use explicitly:
In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.
To be more specific this will also result in an exit code of 1:
any other object is printed to stderr and results in an exit code of 1
There are 3 approaches, the first as lvc mentioned is using sys.exit
sys.exit('My error message')
The second way is using print, print can write almost anything including an error message
print >>sys.stderr, "fatal error" # Python 2.x
print("fatal error", file=sys.stderr) # Python 3.x
The third way is to rise an exception which I don't like because it can be try-catch
raise SystemExit('error in code want to exit')
it can be ignored like this
try:
raise SystemExit('error in code want to exit')
except:
print("program is still open")
Coming from perl, I've been doing:
if object.bad():
sys.exit(1)To exit a program if something goes wrong. It's occured to me that I should just raise an exception instead.
if object.bad():
raise Exception('object is bad')You might be able to skip importing sys.
It makes the code more reusable and portable, so calling code can catch the exception if they want.
It makes it a lot easier to print something to standard error.
If I'm just inserting something temporarily for debugging purposes, I will also sometimes do:
if object.bad():
assert FalseDifference between exit(0) and exit(1) in Python - Stack Overflow
Is it better to quit a script due to a user input error using sys.exit(1) or raise Error()?
Print a custom error message and gracefully exit
Return vs sys.exit()
Videos
0 and 1 are the exit codes.
exit(0) means a clean exit without any errors / problems
exit(1) means there was some issue / error / problem and that is why the program is exiting.
This is not Python specific and is pretty common. A non-zero exit code is treated as an abnormal exit, and at times, the error code indicates what the problem was. A zero error code means a successful exit.
This is useful for other programs, shell, caller etc. to know what happened with your program and proceed accordingly.
This determines the exit status of the program when it finishes running (generally, 0 for success and 1 for error).
It is not unique to Python, and the exact effect depends on your operating system and how the program is called (though 99% of the time, if you're just running Python scripts, it doesn't matter).
Let's say you have a script that is a simple csv parser but the user enters an invalid filepath for the csv.
Is it better to exit the script with a print("wrong filepath") and then sys.exit(1) or by using raise SomeError("wrong filepath")?
From what I read sys.exit(1) raises an exception also, but it doesn't seem to print a traceback like using raise SomeError("wrong filepath") does. So you can just print the message you want the user to see and exit quietly.
If I were to distribute a script like this to public users. What's the better practice?
0 and 1 are exit codes, and they are not necessarily python specific, in fact they are very common.
exit code (0) means an exit without errors or issues.
exit code (1) means there was some issue / problem which caused the program to exit.
The effect of each of these codes can vary between operating systems, but with Python should be fairly consistent.
0 and 1 are exit codes.
exit code (0) means an exit without an errors or any issues, can be a compile time error or any dependency issue.
exit code (1) means there was some issue which caused the program to exit. For example if your program is running on port :8080 and that port is currently in used or not closed, then you code ends up with exit code 1
sys.exit('Error!')
Note from the docs:
If another type of object is passed, None is equivalent to passing zero, and any other object is printed to sys.stderr and results in an exit code of 1. In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.
They're two different ways of showing messages.
print generally goes to sys.stdout and you know where sys.stderr is going. It's worth knowing the difference between stdin, stdout, and stderr.
stdout should be used for normal program output, whereas stderr should be reserved only for error messages (abnormal program execution). There are utilities for splitting these streams, which allows users of your code to differentiate between normal output and errors.
print can print on any file-like object, including sys.stderr:
print >> sys.stderr, 'My error message'
The advantages of using sys.stderr for errors instead of sys.stdout are:
- If the user redirected
stdoutto a file, they still see errors on the screen. - It's unbuffered, so if
sys.stderris redirected to a log file there is less chance that the program will crash before the error was logged.
It's worth noting that there's a third way you can provide a closing message:
sys.exit('My error message')
This will send a message to stderr and exit.