Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Try-Catch block's code in another class

Tags:

java

exception

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
like image 371
big zero Avatar asked Jan 23 '26 05:01

big zero


2 Answers

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.

like image 64
Tudor Avatar answered Jan 24 '26 18:01

Tudor


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

like image 25
RNJ Avatar answered Jan 24 '26 18:01

RNJ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!