![]() ![]() ![]() ![]() |
Preparations |
Many of the methods in the JNDI packages throw a NamingExceptionwhen 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()and DirContext.search()
return a NamingEnumeration
. 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()
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 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
Package
- NamingException
- CannotProceedException
- CommunicationException
- ConfigurationException
- ContextNotEmptyException
- InsufficientResourcesException
- InterruptedNamingException
- InvalidNameException
- LimitExceededException
![]()
- SizeLimitExceededException
- TimeLimitExceededException
- LinkException
![]()
- LinkLoopException
- MalformedLinkException
- NameAlreadyBoundException
- NameNotFoundException
![]()
- NamingSecurityException
![]()
- AuthenticationException
![]()
- AuthenticationNotSupportedException
- NoPermissionException
- NoInitialContextException
- NotContextException
- OperationNotSupportedException
- PartialResultException
- ReferralException
- ServiceUnavailableException
Exceptions in the javax.naming.directory
Package
- NamingException
- AttributeInUseException
- AttributeModificationException
- InvalidAttributeIdentifierException
- InvalidAttributesException
- InvalidAttributeValueException
- InvalidSearchControlsException
- InvalidSearchFilterException
- NoSuchAttributeException
- SchemaViolationException
Exceptions in the javax.naming.ldap
Package
- NamingException
- ReferralException
- LdapReferralException
![]() ![]() ![]() ![]() |
Preparations |