Creating a list of all Java exceptions

Submitted by olaf on 2015-02-24

For some Java code, I searched for an appropriate error or exception, I could throw. Looking on the web yields some incomplete lists. But it is easy to compile one yourself by extracting it from Oracle’s online documentation Java Platform SE 7 and the embedded frame All Classes

curl --silent http://docs.oracle.com/javase/7/docs/api/allclasses-frame.html |
    perl -n -e 'print "$1\n" if (m/href="(.+(?:exception|error))\.html"/i);'

This prints all the relative links to the classes ending in either Exception or Error and gives this list

java/lang/AbstractMethodError
java/nio/channels/AcceptPendingException
java/security/AccessControlException
java/nio/file/AccessDeniedException
java/rmi/AccessException

javax/xml/xpath/XPathExpressionException
javax/xml/xpath/XPathFactoryConfigurationException
javax/xml/xpath/XPathFunctionException
java/util/zip/ZipError
java/util/zip/ZipException

If you want the exception names only, strip the path (package) from the relative URL with this modified regular expression

href=".*/(.+(?:exception|error))\.html"

yielding

AbstractMethodError
AcceptPendingException
AccessControlException
AccessDeniedException
AccessException

XPathExpressionException
XPathFactoryConfigurationException
XPathFunctionException
ZipError
ZipException

For those preferring Python, although it is a complete script including the page download, it looks similar

import urllib2
import re

page = urllib2.urlopen('http://docs.oracle.com/javase/7/docs/api/allclasses-frame.html')
for line in page:
    m = re.search('href="(.+(?:exception|error))\.html"', line, re.IGNORECASE)
    if m:
        print m.group(1)

Post a comment

All comments are held for moderation; Markdown and basic HTML formatting accepted. If you want to stay anonymous, leave name, e-mail and website empty.