Previous | Next | Trail Map | The Basics | Preparations

Naming Exceptions

Many 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. To handle a particular subclass of NamingException specially, you catch the subclass separately. For example, the following code specially treats the AuthenticationException and its subclasses.

try {
    Context ctx = new InitialContext();
    Object obj = ctx.lookup("somename");
} catch (AuthenticationException e) {
    // attempt to reacquire the 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, then NamingException or one of its appropriate subclasses will be thrown at the time that the method is invoked. If an error occurs but there are some results to be returned, then a NamingEnumeration is returned so that you can get those results. When all of the results are exhausted, invoking NamingEnumeration.hasMore() (in the API reference documentation) will cause a NamingException (or one of its subclasses) to be thrown 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, then the search() will return an enumeration consisting of at most n results. If the number of results exceeds n, then 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 is embedded within the text of this tutorial, the try/catch clauses are usually omitted for the sake of readability. Typically, because only code fragments are shown here, only the lines that are directly useful in illustrating a concept are included. You will see appropriate placements of the try/catch clauses for NamingException if you look in the source files that accompany this tutorial.

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

Following are the exceptions contained in the javax.naming 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

Following are the exceptions in the javax.naming.directory 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

Following are the exceptions in the javax.naming.directory 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