2013-01-01 34 views
0

有3種方法作爲f1,f2和f3。我想從F3返回到F1。有沒有可能從方法返回可以說

假設:

最初F1 F2調用

F2 F3調用。

嘗試catch塊應該用在所有這三個函數中。

的情況是,如果我得到了F3異常話,我應該能夠重返F1。

謝謝。

+2

嘗試/最後是你的朋友。 – Perception

+0

如果我在f3中使用try catch,並且如果我在f3的catch中放入return。然後它返回到f2。我想要的是沒有任何條件檢查它應該能夠返回到f1。 –

+0

您應該執行一些狀態標誌來確定何時從f2返回到f1,但這確實需要進行條件檢查。 –

回答

1
catch(Exception e) { 
return; 
} 

您可以在f2中捕捉到異常並添加回車,以便它將轉到f1。或者只是不要在f2中捕獲異常(只需在f2中添加引發)並讓它傳播到f1。

3

嘗試..

void f1(){ 

try{ 
f2(); 
}catch(Exception er){} 
system.out.println("Exception..."); 

} 

void f2() throws Exception{ 

f3(); 

} 

void f3() throws Exception{ 

//on some condition 
throw new Exception("something failed"); 

} 
+0

我編輯了這個問題,請看看它。謝謝。 –

1

嘗試

public void f1(){ 
    f2(); 
    // f3 failed. other code here 
} 

public void f2(){ 
    try { 
     f3(); 
    } catch (Exception e){ 
     // Log your exception here 
    } 
    return; 
} 

public void f3(){ 
    throw new Exception("Error:"); 
} 
+0

嘗試catch塊應該用在所有功能 –

+0

它不需要。在f2中,您將記錄錯誤並返回f2。然後執行f1的剩餘部分執行 –

0

檢查這樣的事情

void f1() throws Exception { 

    try { 
     f2(); 
    } catch (Exception e) { 
     throw new Exception("Exception Ocuured"); 
    } 


} 

void f2() throws Exception { 
    try { 
     f3(); 
    } catch (Exception e) { 
     throw new Exception("Exception Ocuured"); 
    } 
} 

void f3() throws Exception { 

    try { 

     // Do Some work here 
    } catch (Exception e) { 
     f1(); 


    } 
} 
+0

在f3()的catch塊中調用f1()從頭開始執行f1中的語句。執行後返回f3,f3返回f2,在我們調用f3的語句之後,f2中的語句將被執行,然後返回到f1。這不是我需要的行爲。謝謝。 –

相關問題