Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6055403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:15:40+00:00 2026-05-23T08:15:40+00:00

Using LdapTemplate in Spring LDAP, I have this code: Object object=null; try{ String dn

  • 0

Using LdapTemplate in Spring LDAP, I have this code:

Object object=null;
try{
    String dn = "cn=readers,ou=groups,dc=mycompany, dc=com";
    object = this.ldapTemplate.lookup(dn);
} catch(final NameNotFoundException e){
    // create Object
}

But since I’ve read my Joshua Bloch I know that exceptions should not be used for control flow. Is there a way to look up a dn to see if it exists without throwing an exception if it doesn’t? There must be, but I can’t find it. I’m looking for code that works like this (or similar):

String dn = "cn=readers,ou=groups,dc=mycompany, dc=com";
Object object=this.ldapTemplate.someMethod(dn);
if(object==null){
    // create Object
}

Can anybody help?

BTW: just looking at the JavaDoc won’t help. None of the methods that throw NameNotFoundException say so in the JavaDocs.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:15:41+00:00Added an answer on May 23, 2026 at 8:15 am

    The lookup() method is only supposed to be used when you know for certain that the DN exists. This is normally because you’ve searched for and found a user or group previously, and cached the DN returned from the server.

    If you’re looking for something that might or might not be there, the right API to use is ldapTemplate.search(), with an appropriate filter. This returns a list of results, and in the case where no results are found it returns an empty list rather than throwing an exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using jQuery's UI Tabs. This is my code. <div id=tabs> <ul> <li><a href=#tabs-1>Find a
Using online interfaces to a version control system is a nice way to have
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
I have been following the following post on using multiple ItemTemplates in a ListView
Using Core Data. Let's say we have models for Team and Player. Assume: -Each
Using a site like StackOverflow as an example: I have a table of users,
Using gcc 4.4.3 c89: I am just working on the following code below: I
Using PHP I'm attempting to take an HTML string passed from a WYSIWYG editor
I am using a 3rd-party rotator object, which is providing a smooth, random rotation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.