简体   繁体   中英

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

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 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?

Also, you might want to try using the File.OpenRead method instead. This returns a FileStream so you can pass that directly in to the Image.FromStream method. This would look similar to the following code. (I changed it some to use a using statement and only have a single return statement.)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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