2012-05-09 80 views
1

爲什麼我無法返回從try塊中的url獲取的圖像? 。 對不起,我英語不好:(C#從try-catch塊返回

這是錯誤我得到:

返回語句丟失

public static Image GetExternalImg(string name, string size) 
    { 
     try 
     { 
      // Get the image from the web. 
      WebRequest req = WebRequest.Create(String.Format("http://www.picturesite.com/picture.png", name, size)); 
      // Read the image that we get in a stream. 
      Stream stream = req.GetResponse().GetResponseStream(); 
      // Save the image from the stream that we are rreading. 
      Image img = Image.FromStream(stream); 
      // Save the image to local storage. 
      img.Save(Environment.CurrentDirectory + "\\images\\" + name + ".png"); 
      return img; 
     } 
     catch (Exception) 
     { 

     } 
    } 

任何幫助,將不勝感激,因爲我被困現在:()

回答

0

如果被接受,返回空當圖像加載失敗,您可以:

public static Image GetExternalImg(string name, string size) 
    { 
     try 
     { 
      // Get the image from the web. 
      WebRequest req = WebRequest.Create(String.Format("http://www.picturesite.com/picture.png", name, size)); 
      // Read the image that we get in a stream. 
      Stream stream = req.GetResponse().GetResponseStream(); 
      // Save the image from the stream that we are rreading. 
      Image img = Image.FromStream(stream); 
      // Save the image to local storage. 
      img.Save(Environment.CurrentDirectory + "\\images\\" + name + ".png"); 
      return img; 
     } 
     catch (Exception) 
     { 
       //grab an image from resource 
       return theDefaultImage; 
     } 
    } 

否則,又該方法在異常被捕獲時返回? 作爲一個補充,你不應該像上面那樣隱藏異常,這是一個非常糟糕的做法。

+0

如果發現異常,我想顯示一個來自資源的圖像。我可能嗎? –

+0

當然,嵌入圖像作爲一個ñ嵌入資源,並在這裏看看:http://stackoverflow.com/questions/1192054/load-image-from-resources-in-c-sharp –

+0

謝謝,我要去試試:)。 –

1

由於拋出異常,你需要一個返回語句來處理這種情況。

所有的代碼路徑都必須有一個返回值,並且你沒有。即,輸入try塊,拋出一個異常,然後將其吞入catch中。離開捕捉之後,你沒有回報。

順便說一句,吞嚥頂級Exception類型是邪惡的。不要這樣做。

+0

但如果我這樣做,Visual Studio中告訴我:「無法解析符號‘IMG’ –

+0

@Darkshadw:嗯,這是一個不同的問題都在一起'img'是本地'嘗試''所以當然你不能在'try'之外訪問它,我不能告訴你什麼對你的場景是正確的,考慮消除異常吞嚥,或者如果你不想使用'return null' – jason

+0

而不是使用return null我也可以告訴他類似於:return Properties.Resources.defaultimage.png? –

0

因爲如果您有異常,則不會返回任何內容。要麼你捕獲返回null,要麼完整的方法應該返回null。

您可以使用下面的兩個return語句之一。

public static Image GetExternalImg(string name, string size) 
    { 
     try 
     { 
      // Get the image from the web. 
      WebRequest req = WebRequest.Create(String.Format("http://www.picturesite.com/picture.png", name, size)); 
      // Read the image that we get in a stream. 
      Stream stream = req.GetResponse().GetResponseStream(); 
      // Save the image from the stream that we are rreading. 
      Image img = Image.FromStream(stream); 
      // Save the image to local storage. 
      img.Save(Environment.CurrentDirectory + "\\images\\" + name + ".png"); 
      return img; 
     } 
     catch (Exception) 
     { 
      //return null; 
     } 

     //return null; 
    } 
6

你需要從所有可能的執行路徑返回。

因此,如果您的try失敗,則需要從catch或該函數的結尾返回某些內容。

注:

你真的不應該有空洞catch塊 - 吞嚥異常是一個非常不好的習慣,這使得調試和發現其中的異常源於真的很難。

我會寫的功能爲:

public static Image GetExternalImg(string name, string size) 
{ 
    // Get the image from the web. 
    WebRequest req = WebRequest.Create(String.Format("http://www.picturesite.com/picture.png", name, size)); 
    // Read the image that we get in a stream. 
    Stream stream = req.GetResponse().GetResponseStream(); 
    // Save the image from the stream that we are rreading. 
    Image img = Image.FromStream(stream); 
    // Save the image to local storage. 
    img.Save(Environment.CurrentDirectory + "\\images\\" + name + ".png"); 
    return img; 
} 

如果有在catch塊的東西,無論是登錄之後拋出異常了(throw;),或返回null

0

您的'catch'塊中沒有return語句。

如果img.Save拋出一個異常,您將輸入catch,然後離開catch,並且永遠不會遇到return。例如:

public static Image GetExternalImg(string name, string size) 
{ 
    try 
    { 
     // Code here 
     return img; 
    } 
    catch (Exception) 
    { 
     return null; 
    } 
} 
0

該函數在所有可能的代碼路徑上必須有return。一種可能的代碼路徑是try塊的主體拋出異常,該異常在catch塊中處理。包含catch塊的代碼路徑沒有返回語句,因此是非法的。

catch塊也必須是一個returnthrow聲明

0

這裏亞去:

public static Image GetExternalImg(string name, string size) 
     { 
      try 
      { 
       // Get the image from the web. 
       WebRequest req = WebRequest.Create(String.Format("http://www.picturesite.com/picture.png", name, size)); 
       // Read the image that we get in a stream. 
       Stream stream = req.GetResponse().GetResponseStream(); 
       // Save the image from the stream that we are rreading. 
       Image img = Image.FromStream(stream); 
       // Save the image to local storage. 
       img.Save(Environment.CurrentDirectory + "\\images\\" + name + ".png"); 
       return img; 
      } 
      catch (Exception) 
      { 

      } 
     return null; 

     } 
0

在你的情況,你不能保證將返回圖像。這就是爲什麼你需要返回一些東西或重新拋出異常。

1

您可以從try塊返回一個對象。您必須確保所有執行路徑都返回該對象。

如果我們在一個try塊的圖像:

public Image GetPicture() 
{ 
    try 
    { 
     Image image = GetImageFromDb(); 
     return image; 
    } 
    catch(Exception ex) 
    { 

    } 
} 

如果()控制傳遞到catch塊調用GetImageFromDb期間拋出一個異常。這意味着我們跳過了退貨聲明。當控制權傳遞給調用者時,調用者期待返回值。

var callerVariable = GetPicture(); 

現在我們需要從catch中傳回一個值來執行賦值。因爲我們正在處理引用類型,所以我們可以返回null(假設null是有效的執行狀態)。更新捕捉到

catch(Exception ex) 
{ 
    return null; 
}