Python Return Error Code
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta python exit code 2 Discuss the workings and policies of this site About Us Learn more python script return value to shell about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack python exit with error message Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you,
Python Sys.exit Example
helping each other. Join them; it only takes a minute: Sign up Best practice in python for return value on error vs. success up vote 27 down vote favorite 10 In general, let's say you have a method like the below. def intersect_two_lists(self, list1, list2): if not list1: self.trap_error("union_two_lists: list1 must not be empty.") return False if not python exception exit code list2: self.trap_error("union_two_lists: list2 must not be empty.") return False #http://bytes.com/topic/python/answers/19083-standard return filter(lambda x:x in list1,list2) In this particular method when errors are found, I would not want to return the empty list in this case because that could have been the real answer to this specific method call, I want to return something to indicate the parameters were incorrect. So I returned False on error in this case, and a list otherwise (empty or not). My question is, what is the best practice in areas like this, and not just for lists?Return whatever the heck I want and make sure I document it for a user to read? :-) What do most of you folks do: If on success you were supposed to return True or False and you catch an error? If on success you were supposed to return a list and you catch an error? If on success you were supposed to return a file handle and you catch an error? et cetera python return share|improve this questi
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and python get exit code policies of this site About Us Learn more about Stack Overflow the
Python Exit Code 255
company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users
Python Check Exit Code
Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes http://stackoverflow.com/questions/1630706/best-practice-in-python-for-return-value-on-error-vs-success a minute: Sign up Difference between exit(0) and exit(1) in Python up vote 52 down vote favorite 7 What's the difference between exit(0) and exit(1) in Python? I tried looking around but didn't find a specific question on these lines. If it's already been answered, a link would be sufficient. python exit-code share|improve this question edited Sep 18 '15 at 18:43 Kasper http://stackoverflow.com/questions/9426045/difference-between-exit0-and-exit1-in-python Souren 4,29332661 asked Feb 24 '12 at 5:49 KodeSeeker 1,79784577 3 Here's a link to exit() in the docs, which took me forever to find. –Ryne Everett Apr 4 '14 at 2:08 1 keep in mind: The site module (which is imported automatically during startup, except if the -S command-line option is given) adds several constants to the built-in namespace. They are useful for the interactive interpreter shell and should not be used in programs, instead, use sys.exit() –Udi May 7 '14 at 18:12 add a comment| 5 Answers 5 active oldest votes up vote 91 down vote accepted 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, ca
Pages Local Site Map ------------------------ Rename Page Delete Page ------------------------ ------------------------ Remove Spam Revert to this revision ------------------------ SlideShow User Login Handling Exceptions The simplest way to handle exceptions is with a "try-except" block: 1 (x,y) = (5,0) 2 try: 3 z = x/y 4 except ZeroDivisionError: 5 print "divide by zero" If you wanted to examine the exception https://wiki.python.org/moin/HandlingExceptions from code, you could have: 1 (x,y) = (5,0) 2 try: 3 z = x/y 4 except ZeroDivisionError as e: 5 z = e # representation: " Error: %s
solutions from a community of 418,626 IT Pros & Developers. It's quick & easy. sys.exit() P: n/a Ivan Voras In a code such as: if len(sys.argv) < 2: print "I need arguments!" sys.exit(1) Is sys.exit() really a good choice? Is there something more elegant? (I tried return but it is valid only in a function) -- -- Every sufficiently advanced magic is indistinguishable from technology - Arthur C Anticlarke Jul 18 '05 #1 Post Reply Share this Question 12 Replies P: n/a Duncan Booth "Ivan Voras"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure? Jul 18 '05 #2 P: n/a Peter Hansen Ivan Voras wrote: In a code such as: if len(sys.argv) < 2: print "I need arguments!" sys.exit(1) Is sys.exit() really a good choice? Is there something more elegant? (I tried return but it is valid only in a function) sys.exit() is the proper, defined, cross-platform way to exit from a program and return a value to the calling program. Change your definition of elegant and you could consider it easily the most elegant of all solutions. ;-) -Peter Jul 18 '05 #3 P: n/a Ivan Voras Peter Hansen wrote: Ivan Voras wrote: Is sys.exit() really a good choice? Is there something more elegant? (I tried return but it is valid only in a function) sys.exit() is the proper, defined, cross-platform way to exit from a program and return a value to the calling program. Change your definition of elegant and you could consider it easily the most elegant of all solutions. ;-) Ok. :) (Just for the record: I was looking for something that doesn't require