See All Titles |
![]() ![]() Catching ExceptionsLook at an example that shows how to catch a specific exception message. 1: >>> def zerodivision(x): 2: … return 1/x 3: … 4: >>> def test(x): 5: … try: 6: … print zerodivision(x) 7: … except ZeroDivisionError: 8: … print "You can not divide this number by Zero" 9: … 10: test(0) In line 7, we are specifying the exact exception type that we want to catch. You can also replace lines 7 and 8 from the previous example with the text from the next snippet. The difference is that this new scenario also shows the error message provided by the interpreter. except ZeroDivisionError, error_message: print "You can't divide this number by Zero - ", error_message Besides catching Python standard exceptions, it is also possible to catch user-defined, non-Error exceptions. >>> found = "Item found" >>> def searcher(arg): … if arg == 1: … print "executing the routine." … else: … raise found … >>> try: … searcher() >>> except found: … print "The routine has failed." … else: … print "The routine was successfully concluded" The next example re-raises an exception because the win32pipe module is not present in the system. >>> try: … import win32pipe … except: … raise ImportError, "The module is not available" Traceback (innermost last): File "<stdin>", line 4, in ? ImportError: The module is not available The next example actually shows how to raise the same exception (provided the exception is a class exception). This type of implementation doesn't require you to know the name of the exception being raised. >>> import sys >>> try: … import win32pipe … except: … raise sys.exc_value Traceback (innermost last): File "<stdin>", line 4, in ? ImportError: No module named win32pipe The following code catches an IOError exception and raises a SystemExit exception by using the sys.exit() function. >>> import sys >>> try: … file = open("file.txt") … except IOError: … print "Error opening file for reading" … sys.exit(0) Catching Standard ErrorsThe errno module makes available the standard errno system symbols, which can be used to check the meaning of an error. >>> import errno >>> try: >>> file = open("test.py") >>> except IOError, (errcode, errmsg): >>> if errcode == errno.ENOENT: >>> print "File does not exist!" >>> You can check the entire list of error symbols by typing, >>> import errno >>> dir(errno) ['E2BIG', 'EACCES', 'EADDRINUSE', 'EADDRNOTAVAIL', EAFNOSUPPORT', 'EAGAIN', 'EALREADY', 'EBADF', 'EBUSY', 'ECHILD', 'ECONNABORTED', 'ECONNREFUSED', 'ECONNRESET', 'EDEADLK', 'EDEADLOCK', 'EDESTADDRREQ', 'EDOM', 'EDQUOT', 'EEXIST', 'EFAULT', 'EFBIG', 'EHOSTDOWN', 'EHOSTUNREACH', 'EILSEQ', 'EINPROGRESS', 'EINTR', 'EINVAL', 'EIO', 'EISCONN', 'EISDIR', 'ELOOP', 'EMFILE', 'EMLINK', 'EMSGSIZE', 'ENAMETOOLONG', 'ENETDOWN', 'ENETRESET', 'ENETUNREACH', 'ENFILE', 'ENOBUFS', 'ENODEV', 'ENOENT', 'ENOEXEC', 'ENOLCK', 'ENOMEM', 'ENOPROTOOPT', 'ENOSPC', 'ENOSYS', 'ENOTCONN', 'ENOTDIR', 'ENOTEMPTY', 'ENOTSOCK', 'ENOTTY', 'ENXIO', 'EOPNOTSUPP', 'EPERM', 'EPFNOSUPPORT', 'EPIPE', 'EPROTONOSUPPORT', 'EPROTOTYPE', 'ERANGE', 'EREMOTE', 'EROFS', 'ESHUTDOWN', 'ESOCKTNOSUPPORT', 'ESPIPE', 'ESRCH', 'ESTALE', 'ETIMEDOUT', 'ETOOMANYREFS', 'EUSERS', 'EWOULDBLOCK', 'EXDEV', 'WSABASEERR', 'WSAEACCES', 'WSAEADDRINUSE', 'WSAEADDRNOTAVAIL', 'WSAEAFNOSUPPORT', 'WSAEALREADY', 'WSAEBADF', 'WSAECONNABORTED', 'WSAECONNREFUSED', 'WSAECONNRESET', 'WSAEDESTADDRREQ', 'WSAEDISCON', 'WSAEDQUOT', 'WSAEFAULT', 'WSAEHOSTDOWN', 'WSAEHOSTUNREACH', 'WSAEINPROGRESS', 'WSAEINTR', 'WSAEINVAL', 'WSAEISCONN', 'WSAELOOP', 'WSAEMFILE', 'WSAEMSGSIZE', 'WSAENAMETOOLONG', 'WSAENETDOWN', 'WSAENETRESET', 'WSAENETUNREACH', 'WSAENOBUFS', 'WSAENOPROTOOPT', 'WSAENOTCONN', 'WSAENOTEMPTY', 'WSAENOTSOCK', 'WSAEOPNOTSUPP', 'WSAEPFNOSUPPORT', 'WSAEPROCLIM', 'WSAEPROTONOSUPPORT', 'WSAEPROTOTYPE', 'WSAEREMOTE', 'WSAESHUTDOWN', 'WSAESOCKTNOSUPPORT', 'WSAESTALE', 'WSAETIMEDOUT', 'WSAETOOMANYREFS', 'WSAEUSERS', 'WSAEWOULDBLOCK', 'WSANOTINITIALISED', 'WSASYSNOTREADY', 'WSAVERNOTSUPPORTED', '__doc__', '__name__', 'errorcode'] Use the os.strerror() function to \ retrieve the system message associated to a specific error symbol. >>> import os, errno >>> os.strerror(errno.EPERM) "Operation not permitted"
|
Index terms contained in this sectioncatchingexceptions 2nd displaying error symbols errno module error symbols viewing exceptions catching 2nd functions os.sterror() messages system retrieving modules errno os.sterror() function retrieving system messages system messages retrieving viewing error symbols |
© 2002, O'Reilly & Associates, Inc. |