Error Handling Python 2.4
never needs to be imported explicitly: the exceptions are provided in the built-in namespace as well as the exceptions module. Note: In past versions of Python string exceptions were supported. In Python 1.5 and newer versions, all standard exceptions have been converted to class objects and users are encouraged to do the same. String exceptions will raise a PendingDeprecationWarning. In future versions, support for string exceptions will be removed. Two distinct string objects with the same value are considered different exceptions. This is done to force programmers to use exception names rather than their string value when specifying exception handlers. The string value of all built-in exceptions is their name, but this is not a requirement for user-defined exceptions or exceptions defined by library modules. For class exceptions, in a try statement with an except clause that mentions a particular class, that clause also handles any exception classes derived from that class (but not exception classes from which it is derived). Two exception classes that are not related via subclassing are never equivalent, even if they have the same name. The built-in exceptions listed below can be generated by the interpreter or built-in functions. Except where mentioned, they have an ``associated value'' indicating the detailed cause of the error. This may be a string or a tuple containing several items of information (e.g., an error code and a string explaining the code). The associated value is the second argument to the raise statement. For string exceptions, the associated value itself will be stored in the variable named as the second argument of the except clause (if any). For class exceptions, that variable receives the exception instance. If the exception class is derived from the standard root class Exception, the associated value is present as the exception instance's args attribute, and possibly on other attributes as well. User code can raise built-in exceptions. This can be used to test an exception handler or to report an error condition ``just like'' the situation in which the interpreter raises the s
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 Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up python try:except:finally up vote 21 down vote favorite 4 # Open new file to write file = None try: file = https://docs.python.org/2.4/lib/module-exceptions.html open(filePath, 'w') except IOError: msg = ("Unable to create file on disk.") file.close() return finally: file.write("Hello World!") file.close() The above code is ripped from a function. One of the user's system is reporting an error in line: file.write("Hello World!") error: AttributeError: 'NoneType' object has no attribute 'write' Question is, If python is failed to open given file, 'except' block executes and it has to return, but control is getting transferred to the line that http://stackoverflow.com/questions/7777456/python-tryexceptfinally is throwing given error. The value of 'file' variable is 'None'. Any pointers? python exception file-io try-catch share|improve this question edited Oct 15 '11 at 14:54 Sreenath Nannat 9811716 asked Oct 15 '11 at 11:12 user354051 1441513 add a comment| 8 Answers 8 active oldest votes up vote 41 down vote accepted You shouldn't be writing to the file in the finally block as any exceptions raised there will not be caught by the except block. The except bock executes if there is an exception raised by the try block. The finally block always executes whatever happens. Also, there shouldn't be any need for initialising the file variable to none. The use of return in the except block will not skip the finally block. By its very nature it cannot be skipped, that's why you want to put your "clean-up" code in there (i.e. closing files). So, if you want to use try:except:finally, you should be doing something like this: try: f = open("file", "w") try: f.write('Hello World!') finally: f.close() except IOError: print 'oops!' A much cleaner way of doing this is using the with statement: try: with open("output", "w") as outfile: outfile.write('Hello World') except IOError: print 'oops!' share|improve this answer edited Oct 15 '11 at 12:14 answered Oct 15 '11 at 11:14 Acorn 24.6k1581140 6 +1 for wi
and get tips & solutions from a community of 418,513 IT Pros & Developers. It's quick & easy. Exception difference 2.4 ==> 2.5 https://bytes.com/topic/python/answers/855273-exception-difference-2-4-2-5-a P: n/a D'Arcy J.M. Cain I am having a strange problem and I can't seem to zero in on it. I am also having trouble reducing it to a small enough snippet that I can post here. I think that I am doing what the more complex script does but none of my attempts fail. So, here is a description just in case someone error handling has seen something that smells like this and can suggest some areas to do further poking. I have a class that subclasses xmlrpcserver.RequestHandler. It has a method that takes a function name that it looks up with getattr. It then calls the looked up method in this try/except block: try: return server_method(pkt) except Exception, failure: syslog(LOG_WARNING, "%s, %s" % (Exception, failure)) self.print_tb(sys.exc_info()) try: fault error handling python = self.faultConverter.errorToFault(failure) except Exception, err: syslog(LOG_ERR, "Error processing failure: %r" % err) fault = xmlrpclib.Fault(8002, "Internal error") syslog(LOG_DEBUG, "Converted failure %r to fault %r (%r)" % \ (failure, fault, failure)) if fault.faultCode < 10000: syslog(LOG_ERR, "Unconverted fault: %r" % failure) return fault This class is then subclassed by another class that adds the methods that are to be looked up. Those methods may raise exceptions if there are errors. Under Python 2.4 this works fine. If an exception is raised in the looked up method it gets handled by this code just fine. Under 2.5, however, the exception is not caught here. It's as if there was no try/except here at all. I'm not sure if I have left off some critical piece of information or just littered this post with red herrings. I'm open to both reasoned suggestions as well as wild speculation. -- D'Arcy J.M. Cain