Try - Catch 块中未处理的异常
我已经查看了与此问题相关的其他问题,但我没有看到对我有帮助的答案(或者可能更多到我理解的程度)。
此代码:
public static Bitmap GetLibraryObjectImage(Guid guid) {
try {
string tempPath = GetLibraryObjectImagePath(guid);
if (tempPath != string.Empty) {
var bytes = File.ReadAllBytes(tempPath);
var ms = new MemoryStream(bytes);
return (Bitmap)Image.FromStream(ms);
}
}
catch {
return (Bitmap)Image.FromFile(Application.StartupPath + @"\na.bmp");
}
return (Bitmap)Image.FromFile(Application.StartupPath + @"\na.bmp");
}
旨在处理找不到图像文件或以其他方式无效的任何情况。我还没有确定任何特定的异常类型,希望它能捕获任何内容。
对于我的一位用户,它抛出了这个异常:
参数无效。
在 System.Drawing.Image.FromStream(Stream 流、布尔值 useEmbeddedColorManagement、布尔值 validateImageData) 在 System.Drawing.Image.FromStream(Stream 流)
在 ScruffyDuck.AirportDesignEditor.Helpers.U.GetLibraryObjectImage(Guid 指南)
我之前在处理图像的 try-catch 块的其他情况下见过这种情况。我认为上面的 try-catch 会得到一切,包括非托管异常,但也许我错了。
我不知道导致异常的情况,但我真的不希望我的应用程序崩溃并烧毁,即使我试图解决问题。它被我的全局异常处理程序捕获,但当然那时已经有点晚了。
非常感谢您对避免这种情况的任何见解
I have looked at the other questions related to this problem but I do not see answer that helps me (or perhaps more to the point that I understand).
This code:
public static Bitmap GetLibraryObjectImage(Guid guid) {
try {
string tempPath = GetLibraryObjectImagePath(guid);
if (tempPath != string.Empty) {
var bytes = File.ReadAllBytes(tempPath);
var ms = new MemoryStream(bytes);
return (Bitmap)Image.FromStream(ms);
}
}
catch {
return (Bitmap)Image.FromFile(Application.StartupPath + @"\na.bmp");
}
return (Bitmap)Image.FromFile(Application.StartupPath + @"\na.bmp");
}
is intended to deal with any situation in which the image file is not found or is invalid in some other way. I have not identified any specific exception type in the hope that it will catch anything.
For one of my users it threw this exception:
Invalid Parameter.
At System.Drawing.Image.FromStream(Stream
stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
At System.Drawing.Image.FromStream(Stream stream)
At ScruffyDuck.AirportDesignEditor.Helpers.U.GetLibraryObjectImage(Guid
guid)
I have seen this before in other situations in try-catch blocks where handling images. I thought that the above try-catch would get everything including unmanaged exceptions but perhaps I am wrong.
I have no idea regarding the circumstances causing the exception but I really don't want my app to crash and burn even if I am trying to manage the problems. It was caught by my global exception handler but of course by then it is a bit late.
Many thanks for any insight into avoiding this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 @KeithS 在评论中提到的,堆栈跟踪显示异常源自
Image.FromStream
方法。您是否已验证导致该调用的所有代码是否正确返回预期数据?文件路径必须存在(否则File.ReadAllBytes
会引发异常),并且MemoryStream
构造函数返回有效流(否则会引发异常),但是有没有可能内存流中的数据实际上不是图像?另外,您可能想尝试使用
File.OpenRead
方法。这会返回一个FileStream
,因此您可以将其直接传递到Image.FromStream
方法。这看起来类似于以下代码。 (我将其更改为使用using
语句,并且只有一个 return 语句。)As @KeithS mentioned in the comments, the stack trace is showing that the exception is originating in the
Image.FromStream
method. Have you verified that all of the code leading up to that call is correctly returning the expected data? The file path must exist (or theFile.ReadAllBytes
would have thrown an exception) and theMemoryStream
constructor is returning a valid stream (or it would have thrown an exception) but is it possible that the data in the memory stream isn't actually an image?Also, you might want to try using the
File.OpenRead
method instead. This returns aFileStream
so you can pass that directly in to theImage.FromStream
method. This would look similar to the following code. (I changed it some to use ausing
statement and only have a single return statement.)