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.
  • 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
                              • 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
                                S Douglas
                                wrote on last edited by
                                #21

                                Chris Maunder wrote:

                                and using something like UnsupportedFormatException is too hard?

                                Just be glad its not the usual, "Error occurred", Good luck figuring it out. :sigh:


                                Common sense is admitting there is cause and effect and that you can exert some control over what you understand.

                                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

                                  K Offline
                                  K Offline
                                  Kirill Illenseer
                                  wrote on last edited by
                                  #22

                                  Looks like it's the same reason why renaming an EXE to COM and trying to load it generates an "Out of memory" error: Implementation details leaking to the surface.

                                  S 1 Reply 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

                                    S Offline
                                    S Offline
                                    Sentenryu
                                    wrote on last edited by
                                    #23

                                    Extensions aren't trustworthy, nothing guarantees the file is in the format it says it's on. The developer himself might have changed the extension to better suit his application, as evidenced by the shitload of formats that are just XML files, compressed or not, with a different extension (like SVG). As for the first few bytes, many formats have common prefixes (there's 2 or 3 exemples on the list you linked, but there's more), some of them don't even require the prefix to be present and others (like SVG) are encoded as text that can have yet another prefix (BOM). It's sad, but we can't trust the format markers when dealing with multiple formats.

                                    1 Reply Last reply
                                    0
                                    • K Kirill Illenseer

                                      Looks like it's the same reason why renaming an EXE to COM and trying to load it generates an "Out of memory" error: Implementation details leaking to the surface.

                                      S Offline
                                      S Offline
                                      Sentenryu
                                      wrote on last edited by
                                      #24

                                      I mean, not really? COM files have a size limit because they behave different from EXE files (or rather, EXE files behave different, since COM is the original). When you try to load a COM file that exceeds this limit you're really running out of memory, before the code can even start to be executed. Could they read the file size and predict that? they couldn't (or just didn't, hard to say with stuff this old) when COM files where actually being used, so the current included loaders also don't. You don't really want to mess with those kinds of legacy systems, specially when they are useless for current developments and the alternative doesn't share the concerns. [COM file - Wikipedia](https://en.wikipedia.org/wiki/COM\_file#MS-DOS\_binary\_format)

                                      1 Reply 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

                                        B Offline
                                        B Offline
                                        Bruce Patin
                                        wrote on last edited by
                                        #25

                                        I grew up learning: "If at first you don't succeed, try, try again." That philosophy has done well for me. Now, when I suggest it to my teenage son, I get "The definition of insanity is trying the same thing over and over and expecting a different result." It's really a way to get out of trying to do a good job.

                                        L 1 Reply Last reply
                                        0
                                        • B Bruce Patin

                                          I grew up learning: "If at first you don't succeed, try, try again." That philosophy has done well for me. Now, when I suggest it to my teenage son, I get "The definition of insanity is trying the same thing over and over and expecting a different result." It's really a way to get out of trying to do a good job.

                                          L Offline
                                          L Offline
                                          littleGreenDude
                                          wrote on last edited by
                                          #26

                                          I prefer the "Theory of one" If it works for one case, it must work for them all. :-D

                                          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