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. General Programming
  3. C#
  4. Exceptions

Exceptions

Scheduled Pinned Locked Moved C#
helpquestion
17 Posts 7 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.
  • S Offline
    S Offline
    Simon_Whale
    wrote on last edited by
    #1

    I have an existing code base, which we catch exceptions etc. Does anyone know if there is a way to list all the exceptions that either the code catches or raises so that I can do a review? reason for the review is the result of a pen test, which they flagged that the responses had too much details inside the exception.

    Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

    C OriginalGriffO L L L 7 Replies Last reply
    0
    • S Simon_Whale

      I have an existing code base, which we catch exceptions etc. Does anyone know if there is a way to list all the exceptions that either the code catches or raises so that I can do a review? reason for the review is the result of a pen test, which they flagged that the responses had too much details inside the exception.

      Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

      C Offline
      C Offline
      CHill60
      wrote on last edited by
      #2

      I'd probably use Notepad++ to do a File Content search for "Catch" and "Throw" on my project folder

      S 1 Reply Last reply
      0
      • S Simon_Whale

        I have an existing code base, which we catch exceptions etc. Does anyone know if there is a way to list all the exceptions that either the code catches or raises so that I can do a review? reason for the review is the result of a pen test, which they flagged that the responses had too much details inside the exception.

        Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        VS can do a "Find in files" across a whole project or solution, and that can be a regex. So "\scatch\s" and "\sthrow\s" would find and list them all in the "Find" pane (which can be copied to the clipboard) or double clicked to go to each in turn)

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        S 1 Reply Last reply
        0
        • C CHill60

          I'd probably use Notepad++ to do a File Content search for "Catch" and "Throw" on my project folder

          S Offline
          S Offline
          Simon_Whale
          wrote on last edited by
          #4

          Thanks, was wondering there was a clever way to do this but sadly not. Thanks for you time :thumbsup:

          Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            VS can do a "Find in files" across a whole project or solution, and that can be a regex. So "\scatch\s" and "\sthrow\s" would find and list them all in the "Find" pane (which can be copied to the clipboard) or double clicked to go to each in turn)

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

            S Offline
            S Offline
            Simon_Whale
            wrote on last edited by
            #5

            Thanks, was wondering there was a clever way to do this but sadly not. Thanks for you time :thumbsup:

            Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

            1 Reply Last reply
            0
            • S Simon_Whale

              I have an existing code base, which we catch exceptions etc. Does anyone know if there is a way to list all the exceptions that either the code catches or raises so that I can do a review? reason for the review is the result of a pen test, which they flagged that the responses had too much details inside the exception.

              Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              In my experience the most interesting exceptions are the ones we do not throw or catch ourselves, but we get for free by calling some CLR class. Example: Image.FromFile() can throw an OutOfMemoryException when the file's format isn't recognized! Now how do you make sure your code base handles that properly???

              Luc Pattyn [My Articles] The Windows 11 "taskbar" is disgusting. It should be at the left of the screen, with real icons, with text, progress, etc. They downgraded my developer PC to a bloody iPhone.

              S 1 Reply Last reply
              0
              • S Simon_Whale

                I have an existing code base, which we catch exceptions etc. Does anyone know if there is a way to list all the exceptions that either the code catches or raises so that I can do a review? reason for the review is the result of a pen test, which they flagged that the responses had too much details inside the exception.

                Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Log them in FirstChanceException. Have a white / black dictionary / list as you work through them.

                Quote:

                This event is only a notification. Handling this event does not handle the exception or affect subsequent exception handling in any way. After the event has been raised and event handlers have been invoked, the common language runtime (CLR) begins to search for a handler for the exception. FirstChanceException provides the application domain with a first chance to examine any managed exception.

                [AppDomain.FirstChanceException Event (System) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.appdomain.firstchanceexception?view=net-6.0)

                "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                S 1 Reply Last reply
                0
                • S Simon_Whale

                  I have an existing code base, which we catch exceptions etc. Does anyone know if there is a way to list all the exceptions that either the code catches or raises so that I can do a review? reason for the review is the result of a pen test, which they flagged that the responses had too much details inside the exception.

                  Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

                  L Offline
                  L Offline
                  lmoelleb
                  wrote on last edited by
                  #8

                  Look at the points where you expose data from the backend (typically a REST service or similar). If an exception is raised, you generally speaking just return "500 Internal Server error" without any further details - and (very important) log those details on the server. In case you use exception to trigger responses like like "404 NOT FOUND" you need to make sure they do not show stack traces or internal details as well. This is typically done in a central place in the response handler of the API so it will automatically kick in for any exception raised - you need to write this code and test it so you trust it no matter which exception is raised from the lower levels of the code. Then you do not need to hunt your entire code base for possible exceptions - which is an impossible task anyway as some system exceptions can be raised from anywhere. Java tried to get the code to declare all exceptions so you could track it down (checked exceptions) but it was not a great success. UPDATE: And make sure you only catch exceptions in 2 cases: 1) When you can FIX the issue - i.e. no error will be returned to the client, and hence no information will be leaked. Example: You want to read some data from a disk cache. If the read throws an exception catch it and recalculate the data. 2) When you can throw an exception with more details. Example: You try to read the data but the exception just says "Access denied". You know what you where trying to read and which user you where running as, so catch it and throw a new exception adding these datails - keeping the original exception as inner exception.

                  L 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    In my experience the most interesting exceptions are the ones we do not throw or catch ourselves, but we get for free by calling some CLR class. Example: Image.FromFile() can throw an OutOfMemoryException when the file's format isn't recognized! Now how do you make sure your code base handles that properly???

                    Luc Pattyn [My Articles] The Windows 11 "taskbar" is disgusting. It should be at the left of the screen, with real icons, with text, progress, etc. They downgraded my developer PC to a bloody iPhone.

                    S Offline
                    S Offline
                    Simon_Whale
                    wrote on last edited by
                    #9

                    Thanks Luc, the reason I was asking is that we have a task from the security team who pen tested a platform we released. to review all the messages that are returned by the platform to mobile devices. this main api in this platform is large and requires a lot of reading and was wondering if I could in a automated fashion return on the exceptions rather than used human eye.

                    Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

                    1 Reply Last reply
                    0
                    • L Lost User

                      Log them in FirstChanceException. Have a white / black dictionary / list as you work through them.

                      Quote:

                      This event is only a notification. Handling this event does not handle the exception or affect subsequent exception handling in any way. After the event has been raised and event handlers have been invoked, the common language runtime (CLR) begins to search for a handler for the exception. FirstChanceException provides the application domain with a first chance to examine any managed exception.

                      [AppDomain.FirstChanceException Event (System) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.appdomain.firstchanceexception?view=net-6.0)

                      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                      S Offline
                      S Offline
                      Simon_Whale
                      wrote on last edited by
                      #10

                      Thanks Gerry I'll have a look into that

                      Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

                      1 Reply Last reply
                      0
                      • S Simon_Whale

                        I have an existing code base, which we catch exceptions etc. Does anyone know if there is a way to list all the exceptions that either the code catches or raises so that I can do a review? reason for the review is the result of a pen test, which they flagged that the responses had too much details inside the exception.

                        Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        If you go and do a "replace all".. ..then have your exceptions inherit from a custom class inheriting from Exception. For logging, use FirstChanceException as suggested by Gerry; it will show a lot more than the exceptions you throw yourself.

                        Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                        1 Reply Last reply
                        0
                        • L lmoelleb

                          Look at the points where you expose data from the backend (typically a REST service or similar). If an exception is raised, you generally speaking just return "500 Internal Server error" without any further details - and (very important) log those details on the server. In case you use exception to trigger responses like like "404 NOT FOUND" you need to make sure they do not show stack traces or internal details as well. This is typically done in a central place in the response handler of the API so it will automatically kick in for any exception raised - you need to write this code and test it so you trust it no matter which exception is raised from the lower levels of the code. Then you do not need to hunt your entire code base for possible exceptions - which is an impossible task anyway as some system exceptions can be raised from anywhere. Java tried to get the code to declare all exceptions so you could track it down (checked exceptions) but it was not a great success. UPDATE: And make sure you only catch exceptions in 2 cases: 1) When you can FIX the issue - i.e. no error will be returned to the client, and hence no information will be leaked. Example: You want to read some data from a disk cache. If the read throws an exception catch it and recalculate the data. 2) When you can throw an exception with more details. Example: You try to read the data but the exception just says "Access denied". You know what you where trying to read and which user you where running as, so catch it and throw a new exception adding these datails - keeping the original exception as inner exception.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          Isn't there an option to hide that?

                          Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                          L 1 Reply Last reply
                          0
                          • L Lost User

                            Isn't there an option to hide that?

                            Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                            L Offline
                            L Offline
                            lmoelleb
                            wrote on last edited by
                            #13

                            Probably, but hard to say without any details. I am surprised it is not the default behavior of whatever framework he is using, so maybe the default was just changed to make it easier to debug. In ASP.NET you do for example get the "to read the error details, change this setting" message - and someone not understanding the impact might just have followed these instructions. In that case the only thing he needs to do is revert that setting. But I am not going to google the solution to every imaginable framework out there, just because the poster forgot to mention what they are using. :)

                            L 1 Reply Last reply
                            0
                            • L lmoelleb

                              Probably, but hard to say without any details. I am surprised it is not the default behavior of whatever framework he is using, so maybe the default was just changed to make it easier to debug. In ASP.NET you do for example get the "to read the error details, change this setting" message - and someone not understanding the impact might just have followed these instructions. In that case the only thing he needs to do is revert that setting. But I am not going to google the solution to every imaginable framework out there, just because the poster forgot to mention what they are using. :)

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              lmoelleb wrote:

                              I am surprised it is not the default behavior of whatever framework he is using, so maybe the default was just changed to make it easier to debug.

                              It is hidden by default because of hackers. Details should be hidden if it is not on localhost.

                              Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                              L 1 Reply Last reply
                              0
                              • L Lost User

                                lmoelleb wrote:

                                I am surprised it is not the default behavior of whatever framework he is using, so maybe the default was just changed to make it easier to debug.

                                It is hidden by default because of hackers. Details should be hidden if it is not on localhost.

                                Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                                L Offline
                                L Offline
                                lmoelleb
                                wrote on last edited by
                                #15

                                Of course. That is kind of what I have been saying the entire time :)

                                L 1 Reply Last reply
                                0
                                • L lmoelleb

                                  Of course. That is kind of what I have been saying the entire time :)

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #16

                                  I'm currently on coffee two. Not entirely alive yet.

                                  Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                                  1 Reply Last reply
                                  0
                                  • S Simon_Whale

                                    I have an existing code base, which we catch exceptions etc. Does anyone know if there is a way to list all the exceptions that either the code catches or raises so that I can do a review? reason for the review is the result of a pen test, which they flagged that the responses had too much details inside the exception.

                                    Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

                                    J Offline
                                    J Offline
                                    jschell
                                    wrote on last edited by
                                    #17

                                    Simon_Whale wrote:

                                    list all the exceptions that either the code catches or raises so that I can do a review

                                    That process would need to find that those in all of the libraries that you use also.

                                    Simon_Whale wrote:

                                    reason for the review is the result of a pen test,

                                    Probably because you have an API and you are returning the exception stack trace as part of the API error handling. The solution to that is the following 1. Catch all exceptions so "catch(Exception e)" in your API layer. 2. Log the exception. You should of course already be doing this. 3. Return an error result from the API. Do not return the exception. Probably 500. But over time you can differentiate this in the error handling code. I would suggest that you also create an id and log that as well and return that as part of the error message. That way you can track an error reported from a caller back to the specific log entry. You can use a GUID but it is going to be more user friendly, especially if a UI is involved, if you create a simple id. It can rotate over time because the context of the error (when reported) is enough to localize in the logs.

                                    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