Python Return Error String
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the python get exception message workings and policies of this site About Us Learn more about
Python Exception Class
Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions
Python Exception Stack Trace
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, helping each other.
Syntax For Generic Except Clause In Python
Join them; it only takes a minute: Sign up Returning error string from a function in python up vote 1 down vote favorite 1 I have a class function in Python that either returns a success or a failure, but in case of a failure I want it to send a specific error string back. I have 3 approaches python print exception in mind: Pass in an variable error_msg to the function originally set to None and in case of an error, it gets set to the error string. eg: if !(foo(self, input, error_msg)): print "no error" else: print error_msg Return a tuple containing a bool and error_msg from the function. I raise an exception in case of an error and catch it in the calling code. But since I don't see exceptions being used often in the codebase I am working on, so was not too sure about taking this approach. What is the Pythonic way of doing this? python python-2.7 python-3.x share|improve this question asked May 8 '13 at 22:48 KT100 4261925 2 Why not exceptions? –Waleed Khan May 8 '13 at 22:50 add a comment| 2 Answers 2 active oldest votes up vote 6 down vote accepted Create your own exception and raise that instead: class MyValidationError(Exception): pass def my_function(): if not foo(): raise MyValidationError("Error message") return 4 You can then call your function as: try: result = my_function() except MyValidationError a
you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions. 8.1. Syntax Errors¶ Syntax errors, also known python custom exception as parsing errors, are perhaps the most common kind of complaint you python try without except get while you are still learning Python: >>> while True print('Hello world') File "
is important to understand some of the basics of Python exception handling. It works somewhat like the Unix errno https://docs.python.org/2/c-api/exceptions.html variable: there is a global indicator (per thread) of the last error that occurred. Most functions don't clear this on success, but will set it to indicate the cause of the error on failure. Most functions also return an error indicator, usually NULL if they are supposed to return a pointer, or -1 if they return an integer (exception: the PyArg_*() functions return 1 for success and 0 for failure). When a function must fail because some function it called failed, it generally doesn't set the error indicator; the function it called already set it. It is responsible for either handling the error python return error and clearing the exception or returning after cleaning up any resources it holds (such as object references or memory allocations); it should not continue normally if it is not prepared to handle the error. If returning due to an error, it is important to indicate to the caller that an error has been set. If the error is not handled or carefully propagated, additional calls into the Python/C API may not behave as intended and may fail in mysterious ways. The error indicator consists of three Python objects corresponding to the Python variables sys.exc_type, sys.exc_value and sys.exc_traceback. API functions exist to interact with the error indicator in various ways. There is a separate error indicator for each thread. void PyErr_PrintEx(intset_sys_last_vars)¶ Print a standard traceback to sys.stderr and clear the error indicator. Call this function only when the error indicator is set. (Otherwise it will cause a fatal error!) If set_sys_last_vars is nonzero, the variables