C#从尝试捕捉块返回
我为什么不能在try块中返回从url获取的图像? 对不起,英语不好 这是我得到的错误: 返回语句丢失C#从尝试捕捉块返回,c#,return,try-catch,C#,Return,Try Catch,我为什么不能在try块中返回从url获取的图像? 对不起,英语不好 这是我得到的错误: 返回语句丢失 任何帮助都将不胜感激,因为我现在陷入困境:(.您需要一个return语句来处理由于引发异常而退出try时的情况 所有代码路径都必须有一个返回,而您有一个没有。即,输入try块,抛出异常,然后在catch中吞下它。离开catch后,您没有返回 顺便说一句,吞咽顶级的异常类型是邪恶的。不要这样做。因为如果有异常,将不会返回任何内容。要么捕获返回null,要么完整方法应返回null 您可以使用下面两个
任何帮助都将不胜感激,因为我现在陷入困境:(.您需要一个return语句来处理由于引发异常而退出try时的情况 所有代码路径都必须有一个返回,而您有一个没有。即,输入try块,抛出异常,然后在catch中吞下它。离开catch后,您没有返回
顺便说一句,吞咽顶级的
异常
类型是邪恶的。不要这样做。因为如果有异常,将不会返回任何内容。要么捕获返回null,要么完整方法应返回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;
}
您需要从所有可能的执行路径返回
因此,如果您的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您的“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
块中,必须有return
或throw
语句如果在图像加载失败时接受返回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;
}
}
否则,当捕获异常时,该方法应该返回什么?
此外,您不应该像上面那样隐藏异常,这是一个非常糟糕的做法。给您:
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;
}
在您的情况下,不保证您将返回图像。这就是为什么您需要返回某些内容或重新引发异常。您可以从try块返回对象。您必须确保所有执行路径都返回该对象
如果我们在try块中获得图像:
public Image GetPicture()
{
try
{
Image image = GetImageFromDb();
return image;
}
catch(Exception ex)
{
}
}
如果在调用GetImageFromDb()期间引发异常,则会将控件传递给catch块。这意味着我们跳过return语句。当控件传递回调用方时,调用方希望返回值
var callerVariable = GetPicture();
现在我们需要从catch返回一个值来执行赋值。因为我们处理的是引用类型,所以可以返回null(假设null是有效的执行状态)。将catch更新为
catch(Exception ex)
{
return null;
}
但如果我这样做,Visual Studio会告诉我:"无法解析符号“IMG”。@这是一个不同的问题。<代码> IMG是“代码<本地>尝试< /COD>”,当然,您不能在代码之外访问它。尝试< <代码> >。我不能告诉您您的方案是对的。请考虑删除异常吞咽,或者如果您不想要,请使用<代码>返回null < /代码>。这样做。除了使用return null,我还可以告诉他类似的内容:return Properties.Resources.defaultimage.png?@Darkshadw:是的。你只需要返回一些东西。如果捕获到异常,我希望显示资源中的图像。我认为可能吗?当然,将图像作为嵌入式资源嵌入,并在此处查看:谢谢,我将尝试一下:)。您仍然存在吞咽顶级异常的问题。这是邪恶的,这个答案并没有指出这是危险的!
catch(Exception ex)
{
return null;
}