Python Error Codes Vs Exceptions
Contents |
Cleaner Python: Use Exceptions Many programmers have had it drilled into their head that exceptions, in any language, should only be used in truly exceptional cases. They're wrong. The Python community's approach python exception handling best practices to exceptions leads to cleaner code that's easier to read. And that's without
Python Return Exception From Function
the monstrous hit to performance commonly associated with exceptions in other languages. EDIT: Updated with more useful exception idioms Using lbyl vs eafp exceptions to write cleaner code? When I talk about "using exceptions", I'm specifically not referring to creating some crazy exception hierarchy for your package and raising exceptions at every possible opportunity. That will python throw error and exit most certainly lead to unmaintainable and difficult to understand code. This notion has been widely discussed and is well summarized on Joel Spolsky's blog. Note: Python avoids much of the tension of the "error codes vs exceptions" argument. Between the ability to return multiple values from a function and the ability to return values of different types (e.g. None or something similar in the error
Python Exception Types
case) the argument is moot. But this is besides the point. The style of exception usage I'm advocating is quite different. In short: take advantage of Python built-ins and standard library modules that already throw exceptions. Exceptions are built in to Python at the lowest levels. In fact, I guarantee your code is already using exceptions, even if not explicitly. Intermezzo: How the for statement works Any time you use for to iterate over an iterable (basically, all sequence types and anything that defines __iter__() or __getitem__()), it needs to know when to stop iterating. Take a look at the code below: words = ['exceptions', 'are', 'useful'] for word in words: print(word) How does for know when it's reached the last element in words and should stop trying to get more items? The answer may surprise you: the list raises a StopIteration exception. In fact, all iterables follow this pattern. When a for statement is first evaluated, it calls iter() on the object being iterated over. This creates an iterator for the object, capable of returning the contents of the object in sequence. For the call to iter() to succeed, the object must either suppor
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company python raise custom exception Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs python raise valueerror Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers,
Python Catch All Exceptions
just like you, helping each other. Join them; it only takes a minute: Sign up Is it better to use an exception or a return code in Python? up vote 34 down vote favorite 11 You may https://jeffknupp.com/blog/2013/02/06/write-cleaner-python-use-exceptions/ know this recommendation from Microsoft about the use of exceptions in .NET: Performance Considerations ... Throw exceptions only for extraordinary conditions, ... In addition, do not throw an exception when a return code is sufficient... (See the whole text at http://msdn.microsoft.com/en-us/library/system.exception.aspx.) As a point of comparison, would you recommend the same for Python code? python performance exception share|improve this question edited Jul 16 '10 at 9:03 Peter Mortensen 10.3k1369107 asked Jul 20 '09 at http://stackoverflow.com/questions/1152541/is-it-better-to-use-an-exception-or-a-return-code-in-python 9:18 luc 19.6k1376130 6 -1: Why are you trying to apply .Net framework library documentation to Python? What made you think this .Net advice applied to Python? Was there some note or hint that this was relevant to Python? –S.Lott Jul 20 '09 at 9:54 29 I think in fairness he is asking whether "do not throw an exception when a return code is sufficient" applies more generally outside the .Net framework too. It's still a reasonable question even if the answer was "no, it's a .Net thing" –Draemon Jul 20 '09 at 10:17 7 This is a very general recommendation which applies for .NET and c++. I just wanted if such a recommendation is also valid for python. I know that Python and .NET are different. but both of them have exception handling. It is a point of comparison –luc Jul 20 '09 at 10:18 @luc: It may be a point of comparison, but the .NET quote doesn't apply to Python at all. Perhaps the question should be fixed to clarify this. –S.Lott Jul 20 '09 at 14:51 add a comment| 6 Answers 6 active oldest votes up vote 31 down vote accepted The pythonic thing to do is to raise and handle exceptions. The excellent book "Python in a nutshell" discusses this in 'Error-
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 as parsing errors, are perhaps the most common https://docs.python.org/2.7/tutorial/errors.html kind of complaint you get while you are still learning Python: >>> while True print 'Hello world' File "