在另一个类中访问Try-Catch块的代码

时间:2012-08-28 11:10:37

标签: java exception

可能是愚蠢的,但我想清楚我对这段代码的技术理解:

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(...) { 
        } 
    } 
}

现在我打电话给另一个班级

public class B {
    A a = new A();
    //here I want to use attributeName 
}
  • 如何在B类中访问A类的成员(在try块中)。
  • 任何处理try块代码以便在另一个类中重用的方法。
  • 我怎么能在另一个班级处理所有这些例外。

我需要修改......

调用对象类型的方法。

public class C{
private String attributeName;

   public String getAttributeName() {
       return attributeName;
   }

public Object method(){
attributeName=lAttribute.getName(); 
}
}
  • 如何将此Object类型方法打印到String(在jsp页面中)...任何输入

4 个答案:

答案 0 :(得分:5)

您需要班级A中的成员和吸气员:

public class A {
   private String attributeName;

   public String getAttributeName() {
       return attributeName;
   }

   public void method(){
        ...

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

然后:

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

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

无法像在原始示例中那样访问方法的私有变量,因为它们仅在方法调用期间存在于堆栈中。

修改:我注意到另一个问题:

  

我怎么能在另一个类中处理所有这些异常。

我假设您想在其他地方调用您的方法并在那里捕获异常。在这种情况下,您可以使用throws关键字来表明您的方法会将异常传递给调用者:

public class A {

    public void method() throws IOException {

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

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

然后,如果其他一些代码调用method,它将被强制处理或进一步传播异常。

答案 1 :(得分:1)

  

我怎么能在另一个类中处理所有这些异常。

在你的调用类中,你可以捕获Throwable(它将捕获所有异常和错误)

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

如果你不想在Java中捕获错误(我只是在乱搞ImageIO并且遇到内存问题时),那么请抓住异常

  

任何处理try块代码以便在另一个类中重用的方法

这里你可以在另一个类中创建一个方法,然后在你的try / catch块中调用它

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


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

您可能想要也可能不想在这里创建新的XYZ。这取决于该对象可能存在还是不存在的状态。

至于最后一个问题,我认为都铎的答案涵盖了这个

答案 2 :(得分:1)

您的问题可能是提取代码模板

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

您只想更改... do stuff ...部分。在这种情况下,你需要 closures ,它们随Java 8一起提供,今天你需要一些非常繁琐的东西:

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

其中RunnableExc将是

interface RunnableExc { void run() throws Exception; }

你可以这样使用它:

tryCatch(new RunnableExc() { public void run() throws Exception {
   ... do stuff ...
}});

答案 3 :(得分:1)

为什么不return呢?

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();
}