Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Why use the correct exception type?

Why use the correct exception type?

Scheduled Pinned Locked Moved The Weird and The Wonderful
winformsgraphicsquestion
29 Posts 20 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chris Maunder
    wrote on last edited by
    #1

    From the docs for Image.FromFile

    Quote:

    Exceptions OutOfMemoryException The file does not have a valid image format. -or- GDI+ does not support the pixel format of the file.

    ...and using something like UnsupportedFormatException is too hard? :doh:

    cheers Chris Maunder

    P P R J S 11 Replies Last reply
    0
    • C Chris Maunder

      From the docs for Image.FromFile

      Quote:

      Exceptions OutOfMemoryException The file does not have a valid image format. -or- GDI+ does not support the pixel format of the file.

      ...and using something like UnsupportedFormatException is too hard? :doh:

      cheers Chris Maunder

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      ... because they thrash round chewing up memory trying to find a valid format? Wouldn't surprise me in the least. [edit] Removed joke icon in light of other messages in this thread. [/edit]

      Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

      C G 2 Replies Last reply
      0
      • C Chris Maunder

        From the docs for Image.FromFile

        Quote:

        Exceptions OutOfMemoryException The file does not have a valid image format. -or- GDI+ does not support the pixel format of the file.

        ...and using something like UnsupportedFormatException is too hard? :doh:

        cheers Chris Maunder

        P Offline
        P Offline
        phil o
        wrote on last edited by
        #3

        Could have been worse... A MysteriousException, or a ConfusingException instead.

        "I'm neither for nor against, on the contrary." John Middle

        S 1 Reply Last reply
        0
        • C Chris Maunder

          From the docs for Image.FromFile

          Quote:

          Exceptions OutOfMemoryException The file does not have a valid image format. -or- GDI+ does not support the pixel format of the file.

          ...and using something like UnsupportedFormatException is too hard? :doh:

          cheers Chris Maunder

          R Offline
          R Offline
          RickZeeland
          wrote on last edited by
          #4

          You could download the .NET source code and improve it :-\ .NET Foundation · GitHub[^]

          1 Reply Last reply
          0
          • P Peter_in_2780

            ... because they thrash round chewing up memory trying to find a valid format? Wouldn't surprise me in the least. [edit] Removed joke icon in light of other messages in this thread. [/edit]

            Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

            C Offline
            C Offline
            Chris Maunder
            wrote on last edited by
            #5

            Pretty much my assumption. "Let's try it and see what breaks".

            cheers Chris Maunder

            R 1 Reply Last reply
            0
            • C Chris Maunder

              From the docs for Image.FromFile

              Quote:

              Exceptions OutOfMemoryException The file does not have a valid image format. -or- GDI+ does not support the pixel format of the file.

              ...and using something like UnsupportedFormatException is too hard? :doh:

              cheers Chris Maunder

              J Offline
              J Offline
              Jorgen Andersson
              wrote on last edited by
              #6

              OutOfMemoryException is what happens, invalid format is the reason for it. This is the recurring question of whether one should catch the error and throw a new different error instead, or if you should let the original error propagate upwards. I prefer the former if I don't have control over the code.

              Wrong is evil and must be defeated. - Jeff Ello

              C 1 Reply Last reply
              0
              • J Jorgen Andersson

                OutOfMemoryException is what happens, invalid format is the reason for it. This is the recurring question of whether one should catch the error and throw a new different error instead, or if you should let the original error propagate upwards. I prefer the former if I don't have control over the code.

                Wrong is evil and must be defeated. - Jeff Ello

                C Offline
                C Offline
                Chris Maunder
                wrote on last edited by
                #7

                I would just prefer they sniff the image file to ensure they can handle it instead of blindly trying to load it and then finding they are out of memory (and I get that they check and abort before using up all memory, but the point remains)

                cheers Chris Maunder

                J 1 Reply Last reply
                0
                • C Chris Maunder

                  I would just prefer they sniff the image file to ensure they can handle it instead of blindly trying to load it and then finding they are out of memory (and I get that they check and abort before using up all memory, but the point remains)

                  cheers Chris Maunder

                  J Offline
                  J Offline
                  Jorgen Andersson
                  wrote on last edited by
                  #8

                  I believe that verifying a compressed image would go through exactly the same steps as parsing it would. This is probably the most efficient way. The error message is still stupid though.

                  Wrong is evil and must be defeated. - Jeff Ello

                  C 1 Reply Last reply
                  0
                  • P Peter_in_2780

                    ... because they thrash round chewing up memory trying to find a valid format? Wouldn't surprise me in the least. [edit] Removed joke icon in light of other messages in this thread. [/edit]

                    Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

                    G Offline
                    G Offline
                    GuyThiebaut
                    wrote on last edited by
                    #9

                    Sounds quite probable to me - I was debugging the call stack for an EF connection yesterday. I happened to be connecting to a SQL Server database, however as System.Data was being referenced the call stack showed all sorts of database connection types(probably the wrong term but you get my drift?) being tried for the connection, including Oracle. It's kind of clever that it takes away me having to specify the type of database EF is connecting to, while at the same time being rather horrifying that it tries different connection types until it succeeds.

                    “That which can be asserted without evidence, can be dismissed without evidence.”

                    ― Christopher Hitchens

                    1 Reply Last reply
                    0
                    • J Jorgen Andersson

                      I believe that verifying a compressed image would go through exactly the same steps as parsing it would. This is probably the most efficient way. The error message is still stupid though.

                      Wrong is evil and must be defeated. - Jeff Ello

                      C Offline
                      C Offline
                      Chris Maunder
                      wrote on last edited by
                      #10

                      In the case that affected me it was an attempt to load an SVG file which I discovered isn't supported. A simple check of filename or the first few bytes of the file would have found this. There's even a handy list[^] they could refer to.

                      cheers Chris Maunder

                      J S 2 Replies Last reply
                      0
                      • C Chris Maunder

                        In the case that affected me it was an attempt to load an SVG file which I discovered isn't supported. A simple check of filename or the first few bytes of the file would have found this. There's even a handy list[^] they could refer to.

                        cheers Chris Maunder

                        J Offline
                        J Offline
                        Jorgen Andersson
                        wrote on last edited by
                        #11

                        :doh: I was told by my boss the other day that my biggest weakness is that I always assume people do thing properly.

                        Wrong is evil and must be defeated. - Jeff Ello

                        C 1 Reply Last reply
                        0
                        • J Jorgen Andersson

                          :doh: I was told by my boss the other day that my biggest weakness is that I always assume people do thing properly.

                          Wrong is evil and must be defeated. - Jeff Ello

                          C Offline
                          C Offline
                          Chris Maunder
                          wrote on last edited by
                          #12

                          :laugh:

                          cheers Chris Maunder

                          1 Reply Last reply
                          0
                          • C Chris Maunder

                            From the docs for Image.FromFile

                            Quote:

                            Exceptions OutOfMemoryException The file does not have a valid image format. -or- GDI+ does not support the pixel format of the file.

                            ...and using something like UnsupportedFormatException is too hard? :doh:

                            cheers Chris Maunder

                            S Offline
                            S Offline
                            Slacker007
                            wrote on last edited by
                            #13

                            Because the actual internal error being thrown is an OutOfMemoryException. That has been my experience, especially when working with LEADTools libraries and images with OCR software. Edit: I have seen this similar thing happen before, and for some reason the image "may" be corrupted and thus causes a memory issue. Then the developer just returns a crap message saying not a valid image format. But in reality, the software kept reading the bytes until it ran out of memory.

                            1 Reply Last reply
                            0
                            • C Chris Maunder

                              From the docs for Image.FromFile

                              Quote:

                              Exceptions OutOfMemoryException The file does not have a valid image format. -or- GDI+ does not support the pixel format of the file.

                              ...and using something like UnsupportedFormatException is too hard? :doh:

                              cheers Chris Maunder

                              B Offline
                              B Offline
                              Bernhard Hiller
                              wrote on last edited by
                              #14

                              Because a useful name for the exception was out-of-the-memory of the developer.

                              Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

                              1 Reply Last reply
                              0
                              • C Chris Maunder

                                From the docs for Image.FromFile

                                Quote:

                                Exceptions OutOfMemoryException The file does not have a valid image format. -or- GDI+ does not support the pixel format of the file.

                                ...and using something like UnsupportedFormatException is too hard? :doh:

                                cheers Chris Maunder

                                F Offline
                                F Offline
                                F ES Sitecore
                                wrote on last edited by
                                #15

                                The GDI classes seem to be simply a .net facade\interface to the actual underlying code and that code only seems to return generic error messages that are of no help at all. Any issue that happens with GDI simply returns "A generic error occured" even if the error has a specific cause. So I'm going to guess that the reason better exceptions are not thrown is simply because the .net part of the equation doesn't know what the problem is as GDI only bubbles up weird or vague error messages.

                                S 1 Reply Last reply
                                0
                                • F F ES Sitecore

                                  The GDI classes seem to be simply a .net facade\interface to the actual underlying code and that code only seems to return generic error messages that are of no help at all. Any issue that happens with GDI simply returns "A generic error occured" even if the error has a specific cause. So I'm going to guess that the reason better exceptions are not thrown is simply because the .net part of the equation doesn't know what the problem is as GDI only bubbles up weird or vague error messages.

                                  S Offline
                                  S Offline
                                  Slacker007
                                  wrote on last edited by
                                  #16

                                  The return exception is known: out of memory. I have a lot of experience with OCR and images, etc. and this is a common error in underlying C++ libraries when reading the bytes of an image, etc. So, the error is factual and known: out of memory error, usually attributed to an corrupt file format. The user friendly message is still mostly accurate here. It should say something like "your image file format is incorrect or corrupted as the software was unable to process it without error." -- or something like that.

                                  1 Reply Last reply
                                  0
                                  • C Chris Maunder

                                    Pretty much my assumption. "Let's try it and see what breaks".

                                    cheers Chris Maunder

                                    R Offline
                                    R Offline
                                    raddevus
                                    wrote on last edited by
                                    #17

                                    Chris Maunder wrote:

                                    "Let's try it and see what breaks".

                                    Now you got my attention. Theory shmeory. Fire it up and try it out. :thumbsup: :laugh: Just like division by zero. People say it can't be done. Well... I at least want to try it a few times. :laugh:

                                    M 1 Reply Last reply
                                    0
                                    • R raddevus

                                      Chris Maunder wrote:

                                      "Let's try it and see what breaks".

                                      Now you got my attention. Theory shmeory. Fire it up and try it out. :thumbsup: :laugh: Just like division by zero. People say it can't be done. Well... I at least want to try it a few times. :laugh:

                                      M Offline
                                      M Offline
                                      Mycroft Holmes
                                      wrote on last edited by
                                      #18

                                      raddevus wrote:

                                      I at least want to try it a few times.

                                      Isn't that the definition of stupid - repeat the same action with the same settings and expect different results!

                                      Never underestimate the power of human stupidity RAH

                                      R B D 3 Replies Last reply
                                      0
                                      • M Mycroft Holmes

                                        raddevus wrote:

                                        I at least want to try it a few times.

                                        Isn't that the definition of stupid - repeat the same action with the same settings and expect different results!

                                        Never underestimate the power of human stupidity RAH

                                        R Offline
                                        R Offline
                                        raddevus
                                        wrote on last edited by
                                        #19

                                        I have found that in the Venn diagram there is no intersection between : Engineer & Humor :rolleyes: I've marked this comment appropriately as joke (as I should have on the other) to indicate that it is an _attempt_ at humor. :laugh:

                                        1 Reply Last reply
                                        0
                                        • C Chris Maunder

                                          From the docs for Image.FromFile

                                          Quote:

                                          Exceptions OutOfMemoryException The file does not have a valid image format. -or- GDI+ does not support the pixel format of the file.

                                          ...and using something like UnsupportedFormatException is too hard? :doh:

                                          cheers Chris Maunder

                                          T Offline
                                          T Offline
                                          Tim Carmichael
                                          wrote on last edited by
                                          #20

                                          In a moment of clarity, I recalled the answer: they are applying the Maunder Minimum. 8)

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups