2012-08-28 24 views
2

可能是也可能是愚蠢的,但我想清楚了我這個代碼的技術理解:訪問try-catch塊的另一個類代碼

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(); 
} 
} 
  • 怎麼會打印此對象類型的方法轉換成String(JSP頁面)...任何輸入

回答

5

你需要在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() { 
     } 
    } 
} 

然後:

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將被迫要麼處理或進一步傳播例外。

+0

如果我想訪問我的lAttribute.getName()值.... –

+0

@big zero:那麼你是否嘗試我發佈的內容?創建一個成員變量。 – Tudor

+0

感謝您的回覆,但我正在使用我的代碼,並修改了我的問題...需要進行任何修改。 –

1

我怎麼能處理所有這些異常在另一個類。

在您的通話類,你可以趕上的Throwable(這將捕獲所有異常和錯誤)

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

,如果你不想捕獲錯誤(IVE只用ImageIO的亂搞的時候這樣做,有在Java的內存問題),然後趕上例外,而不是

任何方式來處理嘗試在其他類重用塊碼

在這裏你可以創建另一個類的方法,然後你的try/catch塊

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


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

您可能會或可能不會想在這裏創造新的XYZ中調用它。它取決於這個對象可能或不可以持有的狀態。

至於最後一個問題,我認爲都鐸的回答涵蓋了這

1

您的問題可能是有關提取代碼模板

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

如果你只是想改變... do stuff ...部分。在這種情況下,你需要關閉,這是與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 ... 
}}); 
+0

你的代碼在這方面值得讚賞,但我使用sdk 1.6.0.35 :(:( –

+0

但我的代碼與任何Java版本編譯! –

+0

在我的問題,第三塊代碼給出,你能分享你的想法如何實現... –

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