簡體   English   中英

C#從try-catch塊返回

[英]C# Return from a try-catch block

為什么我不能從try塊中返回我從url獲得的圖像? 對不起英語不好:(。

這是我得到的錯誤:

退貨聲明丟失

    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)
        {

        }
    }

任何幫助將不勝感激,因為我現在卡住了:(。

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

因此,如果您的try失敗,您需要從函數的catch或end返回一些內容。

注意:

你真的不應該有空的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

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

如果我們在try塊中獲取圖像:

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

    }
}

如果在調用GetImageFromDb()期間拋出異常,則控件將傳遞給catch塊。 這意味着我們跳過return語句。 當控制權傳遞給調用者時,調用者期望返回值。

var callerVariable = GetPicture();

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

catch(Exception ex)
{
  return null;
}

因為拋出異常,退出try時需要一個return語句來處理這種情況。

所有代碼路徑都必須有一個返回,而你有一個不返回。 即,輸入try塊,拋出異常,然后在catch中吞下它。 離開漁獲后,你沒有回報。

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

如果在圖像加載失敗時接受返回null,則可以:

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

否則,捕獲異常時該方法應該返回什么? 另外,你不應該像上面那樣隱藏異常,這是一個非常糟糕的做法。

因為如果您有例外,則不會返回任何內容。 要么捕獲返回null,要么complete方法應返回null。

您可以使用以下兩個返回語句之一。

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

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

如果img.Save拋出異常,你將進入catch ,然后離開catch並且永遠不會return 例如:

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

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

catch塊中必須有returnthrow語句

你走了:

  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;

        }

在您的情況下,您不能保證將返回圖像。 這就是為什么你需要返回一些東西或者拋出異常的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM