2012-05-09 12 views
1

なぜ私はtryブロック内のURLから取得した画像を返すことができませんか? 。 悪い英語:(申し訳ありませんC#try-catchブロックからの返信

これは私が取得エラーです:

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

     } 
    } 

が欠落しています今すぐ:

答えて

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

もちろん、イメージをaとして埋め込みますn埋め込みリソースと見て:http://stackoverflow.com/questions/1192054/load-image-from-resources-in-c-sharp –

+0

ありがとう、私は試してみるつもり:) –

1

例外がスローされたためにtryを終了すると、ケースを処理するreturn文が必要です。

すべてのコードパスには戻り値が必要ですが、そのコードパスには戻り値がありません。つまり、tryブロックを入力して例外をスローし、catch内でそれを飲み込んでください。あなたがキャッチを離れると、あなたはリターンを持っていません。

ところで、トップレベルを飲み込んだExceptionタイプは悪です。それをしないでください。

+0

にキャッチを更新します。しかし、私はそれを行う場合、Visual Studioは私に語った:「シンボル 『IMG』を解決できません –

+0

@Darkshadw:。。まあ、それはすべて一緒に別の問題だ 'img'が'にローカルでありますあなたのシナリオに合ったものがあるかどうかは分かりませんが、例外を取り除くことを検討したり、したくない場合は 'return null'を使用してください – jason

+0

返り値nullを使う代わりに、私にも同様のことを伝えることができます:return Properties.Resources.defaultimage.png? –

0

例外がある場合、何も返されないためです。キャッチするとnullが返されるか、完全なメソッドはnullを返す必要があります。

以下の2つの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を有していなければなりません。 1つの可能なコードパスは、tryブロックの本体がcatchブロックで処理される例外をスローすることです。 catchブロックを含むコードパスにはreturn文がないため、不正です。ヌルを返すために受理された場合、画像がロードに失敗したときにcatchブロックから

はどちらかreturnまたはthrow声明

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ブロック内の画像を取得する場合:例外がGetImageFromDb(呼び出し中にスローされた場合

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

    } 
} 

)制御がcatchブロックに渡されます。これはreturn文をスキップすることを意味します。呼び出し元は、呼び出し元に制御が戻されるときに戻り値を期待します。

ここで、割り当てを実行するために、catchから値を戻す必要があります。参照型を扱うので、nullを返すことができます(nullは有効な実行状態です)。

catch(Exception ex) 
{ 
    return null; 
}