Previous | Next | Trail Map | The Basics | Preparations

Naming Exceptions

Many of the methods in the JNDI packages throw a NamingException(in the API reference documentation)when they need to indicate that the operation requested cannot be performed.

Commonly, you will see a try/catch wrapper around the methods that can throw a NamingException:

try {
    Context ctx = new InitialContext();
    Object obj = ctx.lookup("somename");
} catch (NamingException e) {
    // Handle the error
    System.err.println(e);
}

Exception Class Hierarchy

The JNDI has a rich exception hierarchy stemming from the NamingException class. The class names of the exceptions are self-explanatory and are listed later in this discussion. If you are interested in handling a particular subclass of NamingException specially, you can do so by catching the subclass separately. For example, the following code treats the AuthenticationException and its subclasses specially:

try {
    Context ctx = new InitialContext();
    Object obj = ctx.lookup("somename");
} catch (AuthenticationException e) {
    // attempt to re-acquire authentication information
    ...
} catch (NamingException e) {
    // Handle the error
    System.err.println(e);
}

Enumerations

Operations such as Context.list() (in the API reference documentation) and DirContext.search() (in the API reference documentation) return a NamingEnumeration (in the API reference documentation). In these cases, if an error occurs and no results are returned, NamingException or one of its appropriate subclasses will be thrown at the time the method is invoked. If an error occurs but there are some results to be returned, a NamingEnumeration is returned so that you can get those results. When all the results are exhausted, invoking NamingEnumeration.hasMore() (in the API reference documentation) will throw a NamingException (or one of its subclasses) to indicate the error. At that point, the enumeration becomes invalid and no more methods should be invoked on it.

For example, if you perform a search() and specify a count limit (n) of how many answers to return, the search() will return an enumeration consisting of at most n results. If the number of results exceeds n, when NamingEnumeration.hasMore() is invoked for the n+1 time, a SizeLimitExceededException will be thrown. See the count limit discussion (in the Basics trail) in this trail for sample code.

Examples in this Tutorial

In the inline sample code that you see embedded within the text of this tutorial, the try/catch clauses are usually omitted for the sake of readability. Typically, because we are only showing code fragments, we only include the lines that are directly useful in illustrating a concept. If you look in the source files that accompany this tutorial, you will see appropriate placements of the try/catch clauses for NamingException.

Exceptions in the javax.naming(in the API reference documentation) Package

NamingException(in the API reference documentation)
CannotProceedException(in the API reference documentation)
CommunicationException(in the API reference documentation)
ConfigurationException(in the API reference documentation)
ContextNotEmptyException(in the API reference documentation)
InsufficientResourcesException(in the API reference documentation)
InterruptedNamingException(in the API reference documentation)
InvalidNameException(in the API reference documentation)
LimitExceededException(in the API reference documentation)
SizeLimitExceededException(in the API reference documentation)
TimeLimitExceededException(in the API reference documentation)
LinkException(in the API reference documentation)
LinkLoopException(in the API reference documentation)
MalformedLinkException(in the API reference documentation)
NameAlreadyBoundException(in the API reference documentation)
NameNotFoundException(in the API reference documentation)
NamingSecurityException(in the API reference documentation)
AuthenticationException(in the API reference documentation)
AuthenticationNotSupportedException(in the API reference documentation)
NoPermissionException(in the API reference documentation)
NoInitialContextException(in the API reference documentation)
NotContextException(in the API reference documentation)
OperationNotSupportedException(in the API reference documentation)
PartialResultException(in the API reference documentation)
ReferralException(in the API reference documentation)
ServiceUnavailableException(in the API reference documentation)

Exceptions in the javax.naming.directory(in the API reference documentation) Package

NamingException(in the API reference documentation)
AttributeInUseException(in the API reference documentation)
AttributeModificationException(in the API reference documentation)
InvalidAttributeIdentifierException(in the API reference documentation)
InvalidAttributesException(in the API reference documentation)
InvalidAttributeValueException(in the API reference documentation)
InvalidSearchControlsException(in the API reference documentation)
InvalidSearchFilterException(in the API reference documentation)
NoSuchAttributeException(in the API reference documentation)
SchemaViolationException(in the API reference documentation)

Exceptions in the javax.naming.ldap(in the API reference documentation) Package

NamingException(in the API reference documentation)
ReferralException(in the API reference documentation)
LdapReferralException(in the API reference documentation)


Previous | Next | Trail Map | The Basics | Preparations