2017-01-16 27 views
0

我正在使用web應用程序。我使用aspx頁面作爲api.From函數我調用函數2.兩個函數都嘗試並捕獲塊。如何捕獲異常時跳過進一步的執行

Function1() 
    { 
     try 
     { 
     int b = function2() 
     } 
     catch(Exception ex) 
     { 
     Response.Write(ex.Tostring()); 
     } 

    } 

    public int Function2() 
    { 
     int a= 0; 
     try 
     { 
     a=8; 
     return a; 
     } 
     catch(Exception ex) 
     { 
     Response.Write(ex.Tostring()); 
     return a; 
     } 

    } 

我想跳過進一步執行(function1)如果錯誤在第二function.catch中捕獲我可以在第二個函數的catch塊中使用break。

回答

4

Function2catch塊不returnthrow異常,因此將再次陷入function1catch塊。

Function1() 
{ 
    try 
    { 
     int b = function2() 
    } 
    catch(Exception ex) 
    { 
     Response.Write(ex.Tostring()); 
    } 
} 

public int Function2() 
{ 
    int a= 0; 
    try 
    { 
     a=8; 
     return a; 
    } 
    catch(Exception ex) 
    { 
     Response.Write(ex.Tostring()); 
     throw; //<----- here 
    } 
} 
1

據對break你不能在第二個功能Break停止功能1的進一步執行在給定的例子的文檔。但你可以做這樣的事情。

Function1() 
{ 
    try 
    { 
     int b = function2() 
     if (b = 0) 
      break; // Or maybe a return of an error. 
    } 
    catch(Exception ex) 
    { 
     Response.Write(ex.Tostring()); 
    } 
}