Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Appendix C. Jython Exceptions

Appendix C. Jython Exceptions

The following is a list of Python language exceptions raised by Jython, along with a discussion of their potential Java equivalents. Deprecated exceptions are not included.


ArithmeticError

Abstract base class of all the mathematical exceptions (such as ZeroDivisionError).


AssertionError

Raised by Python’s assert statement, which has the form assert expression, message. If the expression evaluates to false, the exception is raised, with the optional message as additional data. In Java 1.4, an assert statement and an AssertionError have been added to the language.


AttributeError

Raised when an attempt to reference or call an object attribute fails because the object does not have the attribute. The attribute name is the value of the exception. Java generally catches these in compilation, but if you are using reflection, it can either be a NoSuchFieldException or a NoSuchMethodException. If you try to call an attribute of a None value, you’ll get this exception as well, so NullPointerException is also related.


EnvironmentError

Abstract base class for exceptions raised outside Jython (such as OSError and IOError). Usually created with two arguments. The first is placed in an attribute called errno, and the second is in the attribute strerror and is usually the external error message. There is an optional third attribute, filename.


EOFError

Raised when a built-in read function hits the end of a file and hasn’t read any data. Similar to java.io.EOFException.


Exception

Abstract root class of the exception hierarchy; never raised directly. User-defined exceptions should be derived from this class (or one of its children). Unless redefined by the derived class, converting the exception to a string returns the value of all arguments passed to the exception constructor. The arguments are stored in the args attribute.


FloatingPointError

Raised when a floating-point operation fails.


IOError

Raised when an I/O operation goes wrong, similar to Java’s IOException. In Jython, IOError instances generated by the system have filename, errno, and strerror set to None.


ImportError

Raised when an import or from statement cannot find the expected module or Java class. Also fails if the from statement cannot find the specific name it is looking for. The Java equivalent is java.lang.ClassNotFoundException.


IndexError

Raised when a list index is out of bounds; analogous to Java’s ArrayIndexOutOfBoundsException.


KeyError

Raised when a dictionary reference is made to a key that is not in the dictionary. Java does not have a similar exception. Java map instances return null in this case.


KeyboardInterrupt

Raised if the user hits the interrupt key (usually Ctrl-C) under CPython. Jython defines it for compatibility with CPython but is unable to detect the interrupt key because of Java limitations.


LookupError

Abstract base class of all errors caused by a faulty lookup (IndexError and KeyError).


MemoryError

Raised in the event that the system does not have memory to allocate an object, but hasn’t yet crashed. Jython throws it when it manages to catch a Java OutOfMemoryError.


NameError

Raised if a local or global name is not found. Java usually would catch these errors in compilation. If the name is part of an object reference, AttributeError is raised instead.


NotImplementedError

A method should raise this error if its implementation is left to a subclass, the equivalent of the Java keyword abstract when applied to methods.


OSError

Raised primarily by the os module to indicate an error in an operating system call. Under the os, generated OSError instances have meaningful filename and strerror attributes but errno is set to 0.


OverflowError

Raised when an arithmetic result is outside the range of acceptable values for its data type. In Jython, that usually means int values.


RuntimeError

Rarely used exception for an error that doesn’t fit in any of the other categories. In Jython, mostly used for unexpected internal conditions.


StandardError

Abstract child of Exception; base class of all exceptions other than SystemExit.


SyntaxError

Generally raised when modules are interpreted; indicates a syntax error in the Python code. A SyntaxError instance exposes filename, lineno, and offset (column), text, and msg of the error as attributes. Most of these items are caught at compile time in Java.


SystemError

Raised to indicate some kind of non-panic internal error condition.


SystemExit

Raised by the function sys.exit( ). If it is not handled, causes Jython to exit.


TypeError

Raised when attempting to call a function on an invalid type—for example, trying to add a string to an integer. Some of these kinds of errors are caught at compile time in Java; others would indicate a ClassCastException.


UnboundLocalError

Subclass of NameError; specifically raised if a local name is used before it is created.


UnicodeError

Indicates that an exceptional condition has occurred during encoding or decoding Unicode characters.


ValueError

Generic error for other problems with arguments to functions not already covered by TypeError or AttributeError. In practice, the distinctions between those three errors are not always clear-cut. The closest Java relation is IllegalArgumentException.


ZeroDivisionError

Raised when the program attempts to divide by zero. Java would throw java.lang.ArithmeticException.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial