2

May be it could be silly,but I want to clear my the technical understanding of this code:

import netscape.*;//ldap jar
public class A {

    public void method() {
        ...

        try {
            //code is written here.
             LDAPSearchResults lsr = ldi.search(LDAPConnectionInfo.MY_SEARCHBASE,LDAPConnectionInfo.MY_SCOPE,LDAPConnectionInfo.MY_FILTER,null,false);
             while(lsr.hasMoreElements()){
             LDAPEntry findEntry = (LDAPEntry)lsr.nextElement();

        } catch(...) { 
        } 
    } 
}

Now I call another class

public class B {
    A a = new A();
    //here I want to use attributeName 
}
  • How could I access A class's member(in try block) in B class.
  • Any way to handle try block code for reuse in another class.
  • How could I handle all those exception in another class.

Any modification should I need...

Calling method of Object type.

public class C{
private String attributeName;

   public String getAttributeName() {
       return attributeName;
   }

public Object method(){
attributeName=lAttribute.getName(); 
}
}
  • How could print this Object type method into String(in a jsp page)... any inputs

4 Answers 4

6

You'll need a member in class A and a getter:

public class A {
   private String attributeName;

   public String getAttributeName() {
       return attributeName;
   }

   public void method(){
        ...

        try {
            //code is written here.
            attributeName = lAttribute.getName(); 
        }
        catch() { 
        } 
   } 
}

Then:

public class B {
    A a = new A();

    // somewhere
    String str = a.getAttributeName();
}

There's no way to access a method's private variables like you did in the original example, as they only exist on the stack during the method call.

Edit: I noticed another question:

How could I handle all those exception in another class.

I assume you want to call your method somewhere else and catch the exceptions there. In that case you can use the throws keyword to communicate that your method will pass exceptions to the caller:

public class A {

    public void method() throws IOException {

        //code is written here.
        String attributeName = lAttribute.getName(); 
    } 

    public void anotherMethod() {
        try {
            method();
        } catch(IOException ex) {
            ...
        } 
    }
}

then if some other piece of code calls method it will be forced to either handle or further propagate the exception.

10
  • @big zero: So did you try what I posted? To create a member variable.
    – Tudor
    Aug 28, 2012 at 13:10
  • thanks for your reply,but I'm using my code like this & have edited my question... any modification should I need.
    – big zero
    Aug 28, 2012 at 13:26
  • @big zero: You can do it the same way, except you need to declare a member of type LDAPEntry instead of String.
    – Tudor
    Aug 28, 2012 at 13:28
  • But LDAPEntry is the default class comes with that netscape.*,still I should go with you method....?
    – big zero
    Aug 28, 2012 at 13:31
  • @big zero: It doesn't matter. You can use this method for any type.
    – Tudor
    Aug 28, 2012 at 13:36
1

How could I handle all those exception in another class.

In your calling class you can catch Throwable (which will catch all exceptions and errors)

try {
....
}
catch (Throwable t) {
//do something with the throwable.
}

if you do not want to catch Errors (Ive only done this when messing around with ImageIO and had memory problems) in Java then catch Exception instead

Any way to handle try block code for reuse in another class

here you could create a method in another class and then call it within your try /catch block

public class XYX {
    public void methodForTry() throws Exception {
    //do something
    }
}


try {
    new XYZ().methodForTry();
}
catch (Exception e){
}

You may or may not want to create new XYZ here. It depends what state this object may or may not hold.

As to the last questions I think Tudor's answer covers this

1

Your question may be about extracting the code template

try { ... do stuff ... } 
catch (MyFirstException e) { ...handle ... }
catch (MySecondException e) { ...handle ... }
... more catch ...

Where you only want to change the ... do stuff ... part. In that case you'd need closures, which are coming with Java 8, and today you'd need something quite cumbersome, of this sort:

public static void tryCatch(RunnableExc r) {
  try { r.run(); } 
  catch (MyFirstException e) { ...handle ... }
  catch (MySecondException e) { ...handle ... }
  ... more catch ...
}

where RunnableExc would be an

interface RunnableExc { void run() throws Exception; }

and you'd use it this way:

tryCatch(new RunnableExc() { public void run() throws Exception {
   ... do stuff ...
}});
2
  • your code on this context is worthy to be appreciated,but I'm using sdk 1.6.0.35 :( :(
    – big zero
    Aug 30, 2012 at 13:31
  • in my Ques,3rd block of code is given,could you share your idea how does it can be achieved...
    – big zero
    Aug 30, 2012 at 13:53
1

why not return it?

public String method() {
String attributeName
    try {
        //code is written here.
        attributeName = lAttribute.getName(); 
    } catch(...) { 
    } 
return attributeName;
}


public class B {
A a = new A();
String attributeName = a.method();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.