简体   繁体   English

尝试未处理的异常-捕获块

[英]Unhandled Exception from Try - Catch Block

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) 在System.Drawing.Image.FromStream(Stream stream,布尔useEmbeddedColorManagement,布尔validateImageData)在System.Drawing.Image.FromStream(Stream stream)
At ScruffyDuck.AirportDesignEditor.Helpers.U.GetLibraryObjectImage(Guid guid) 在ScruffyDuck.AirportDesignEditor.Helpers.U.GetLibraryObjectImage(Guid guid)

I have seen this before in other situations in try-catch blocks where handling images. 在处理图像的try-catch块中的其他情况下,我之前已经看到过。 I thought that the above try-catch would get everything including unmanaged exceptions but perhaps I am wrong. 我以为上面的try-catch可以获取所有信息,包括非托管异常,但也许我错了。

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 非常感谢您对避免这种情况的见解

As @KeithS mentioned in the comments, the stack trace is showing that the exception is originating in the Image.FromStream method. 正如注释中提到的@KeithS一样,堆栈跟踪显示该异常源自Image.FromStream方法。 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 the File.ReadAllBytes would have thrown an exception) and the MemoryStream 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? 文件路径必须存在(否则File.ReadAllBytes将引发异常)并且MemoryStream构造函数将返回有效流(否则将引发异常),但是内存流中的数据实际上可能不是一个图像?

Also, you might want to try using the File.OpenRead method instead. 另外,您可能想尝试使用File.OpenRead方法。 This returns a FileStream so you can pass that directly in to the Image.FromStream method. 这将返回FileStream因此您可以将其直接传递给Image.FromStream方法。 This would look similar to the following code. 这看起来类似于以下代码。 (I changed it some to use a using statement and only have a single return statement.) (我将其更改为使用using语句,并且只有一个return语句。)

public static Bitmap GetLibraryObjectImage(Guid guid) 
{ 
   Bitmap bitmap = null;

   try
   { 
      string tempPath = GetLibraryObjectImagePath(guid); 
      if (!String.IsNullOrEmpty(tempPath) 
      {
         using (var stream = File.OpenRead(tempPath))
         {
            bitmap = (Bitmap)Image.FromStream(stream);
         }
      } 
   } 
   catch
   { 
      bitmap = (Bitmap)Image.FromFile(Application.StartupPath + @"\na.bmp"); 
   }

   if (bitmap == null)
   {
      bitmap = (Bitmap)Image.FromFile(Application.StartupPath + @"\na.bmp"); 
   }

   return bitmap;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM