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. Catch all but a specific exception

Catch all but a specific exception

Scheduled Pinned Locked Moved C#
data-structuresquestion
13 Posts 8 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.
  • B Offline
    B Offline
    Bernhard Hiller
    wrote on last edited by
    #1

    In a function, I want to catch all exceptions and just log them (they do not really matter), but one specific type of exception must not be handled like this - it must be dealt with in a function farther up the call stack. Currently, I do something like this:

    private void SomeFunction()
    {
    try
    {
    //some code which might cause some exceptions
    }
    catch(UserTriedToCheatException)
    {
    throw;
    }
    catch (Exception ex)
    {
    Log(ex.ToString());
    }
    }

    Though that code works as expected, I am not content with it. That catch-throw looks so terrible. Do you have "nicer" suggestions?

    B R Richard Andrew x64R C 4 Replies Last reply
    0
    • B Bernhard Hiller

      In a function, I want to catch all exceptions and just log them (they do not really matter), but one specific type of exception must not be handled like this - it must be dealt with in a function farther up the call stack. Currently, I do something like this:

      private void SomeFunction()
      {
      try
      {
      //some code which might cause some exceptions
      }
      catch(UserTriedToCheatException)
      {
      throw;
      }
      catch (Exception ex)
      {
      Log(ex.ToString());
      }
      }

      Though that code works as expected, I am not content with it. That catch-throw looks so terrible. Do you have "nicer" suggestions?

      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #2

      Exceptions don't really matter? Hm ... I don't find your construct too bad but the alternative which expresses your intent (imo) slightly better is:

      try {
      // ...
      } catch(Exception ex) {
      if(ex is UserTriedToCheatException) throw;
      else Log(ex);
      }

      B 1 Reply Last reply
      0
      • B Bernhard Hiller

        In a function, I want to catch all exceptions and just log them (they do not really matter), but one specific type of exception must not be handled like this - it must be dealt with in a function farther up the call stack. Currently, I do something like this:

        private void SomeFunction()
        {
        try
        {
        //some code which might cause some exceptions
        }
        catch(UserTriedToCheatException)
        {
        throw;
        }
        catch (Exception ex)
        {
        Log(ex.ToString());
        }
        }

        Though that code works as expected, I am not content with it. That catch-throw looks so terrible. Do you have "nicer" suggestions?

        R Offline
        R Offline
        Rob Philpott
        wrote on last edited by
        #3

        If you're just logging the exception, why do you need an exception handler here at all? Just use the one 'further up the callstack'.

        Regards, Rob Philpott.

        B 1 Reply Last reply
        0
        • R Rob Philpott

          If you're just logging the exception, why do you need an exception handler here at all? Just use the one 'further up the callstack'.

          Regards, Rob Philpott.

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

          Because of other code to be executed or not. Something like this:

          public ReturnType APublicFunction(some params)
          {
          try
          {
          //some code here
          SomeType someValue = aPrivateFunction(other params);
          //some more code here
          }
          catch(Exception ex)
          {
          //do something useful with the exceptions which are caught here
          }
          }

          private SomeType aPrivateFunction(other params)
          {
          //some code here
          SomeFunction();
          //some more code here
          }

          In case of the UserTriedToCheatException, it has to be handled in APublicFunction, and no more other code needs to be executed, while in other cases, the exceptions are not such important, the rest of the functions can still be executed (sufficient fallback functionality provided).

          L S 2 Replies Last reply
          0
          • B BobJanova

            Exceptions don't really matter? Hm ... I don't find your construct too bad but the alternative which expresses your intent (imo) slightly better is:

            try {
            // ...
            } catch(Exception ex) {
            if(ex is UserTriedToCheatException) throw;
            else Log(ex);
            }

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

            Looks different, but not so much better that I will change my code. Thanks anyway.

            R 1 Reply Last reply
            0
            • B Bernhard Hiller

              In a function, I want to catch all exceptions and just log them (they do not really matter), but one specific type of exception must not be handled like this - it must be dealt with in a function farther up the call stack. Currently, I do something like this:

              private void SomeFunction()
              {
              try
              {
              //some code which might cause some exceptions
              }
              catch(UserTriedToCheatException)
              {
              throw;
              }
              catch (Exception ex)
              {
              Log(ex.ToString());
              }
              }

              Though that code works as expected, I am not content with it. That catch-throw looks so terrible. Do you have "nicer" suggestions?

              Richard Andrew x64R Offline
              Richard Andrew x64R Offline
              Richard Andrew x64
              wrote on last edited by
              #6

              Bernhard Hiller wrote:

              That catch-throw looks so terrible.

              In my opinion, it won't look so terrible with some good comments around it!

              The difficult we do right away... ...the impossible takes slightly longer.

              1 Reply Last reply
              0
              • B Bernhard Hiller

                Because of other code to be executed or not. Something like this:

                public ReturnType APublicFunction(some params)
                {
                try
                {
                //some code here
                SomeType someValue = aPrivateFunction(other params);
                //some more code here
                }
                catch(Exception ex)
                {
                //do something useful with the exceptions which are caught here
                }
                }

                private SomeType aPrivateFunction(other params)
                {
                //some code here
                SomeFunction();
                //some more code here
                }

                In case of the UserTriedToCheatException, it has to be handled in APublicFunction, and no more other code needs to be executed, while in other cases, the exceptions are not such important, the rest of the functions can still be executed (sufficient fallback functionality provided).

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

                How about something similar to the way TryParse[^] works?

                if (SomePrivateFunction(out SomeType SomeParamName))
                {
                // insert magic here
                }

                Bastard Programmer from Hell :suss:

                1 Reply Last reply
                0
                • B Bernhard Hiller

                  Looks different, but not so much better that I will change my code. Thanks anyway.

                  R Offline
                  R Offline
                  Ravi Bhavnani
                  wrote on last edited by
                  #8

                  OK, but leave out the else - it's extraneous. :) /ravi

                  My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                  1 Reply Last reply
                  0
                  • B Bernhard Hiller

                    Because of other code to be executed or not. Something like this:

                    public ReturnType APublicFunction(some params)
                    {
                    try
                    {
                    //some code here
                    SomeType someValue = aPrivateFunction(other params);
                    //some more code here
                    }
                    catch(Exception ex)
                    {
                    //do something useful with the exceptions which are caught here
                    }
                    }

                    private SomeType aPrivateFunction(other params)
                    {
                    //some code here
                    SomeFunction();
                    //some more code here
                    }

                    In case of the UserTriedToCheatException, it has to be handled in APublicFunction, and no more other code needs to be executed, while in other cases, the exceptions are not such important, the rest of the functions can still be executed (sufficient fallback functionality provided).

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

                    you can always invoking a Delegate, which will call the registered private method

                    Satish

                    1 Reply Last reply
                    0
                    • B Bernhard Hiller

                      In a function, I want to catch all exceptions and just log them (they do not really matter), but one specific type of exception must not be handled like this - it must be dealt with in a function farther up the call stack. Currently, I do something like this:

                      private void SomeFunction()
                      {
                      try
                      {
                      //some code which might cause some exceptions
                      }
                      catch(UserTriedToCheatException)
                      {
                      throw;
                      }
                      catch (Exception ex)
                      {
                      Log(ex.ToString());
                      }
                      }

                      Though that code works as expected, I am not content with it. That catch-throw looks so terrible. Do you have "nicer" suggestions?

                      C Offline
                      C Offline
                      Cracked Down
                      wrote on last edited by
                      #10

                      Could you please clarify the meaning of up in the hierarchy or the use case where you need such type of exception handling? I am asking this only because I thing your approach could be wrong? well even I could be thinking wrong as well :-D Happy Coding

                      B 1 Reply Last reply
                      0
                      • C Cracked Down

                        Could you please clarify the meaning of up in the hierarchy or the use case where you need such type of exception handling? I am asking this only because I thing your approach could be wrong? well even I could be thinking wrong as well :-D Happy Coding

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

                        For example: a program starts. Many configuration and option files are read, server connections established etc. Some problems might arise, but fallback mechanisms are provided for many cases. But somewhere down in a function, a manipulation of the license is detected - that must be raised to the topmost level, the user must be told "You bad user $€$€!" and the program shuts down immediately.

                        C 1 Reply Last reply
                        0
                        • B Bernhard Hiller

                          For example: a program starts. Many configuration and option files are read, server connections established etc. Some problems might arise, but fallback mechanisms are provided for many cases. But somewhere down in a function, a manipulation of the license is detected - that must be raised to the topmost level, the user must be told "You bad user $€$€!" and the program shuts down immediately.

                          C Offline
                          C Offline
                          Cracked Down
                          wrote on last edited by
                          #12

                          Ok got it! Do following things 1. Build two separate hierarchies for exception 1.1 For exception that you want to catch log e.g. LoggableException 1.2 For exception that you want to you caller to handle e.g. NonLoggableExcptions 2. Only catch the LoggableException exceptions - log in your case 3. Don not catch NonLoggableExcptions - these will be automatically traverse to the caller 4. The try block code should only throw these two exceptions or the derived objects from these exceptions 5. Don not catch Exception, its a bad practice, which says anything could be happen in the code. can it be :-D . this gives bad impression. though I am not sure if any unhanded exception occurs how it is handled in the client server application as I never worked on it hope this helps to you and solve you problem... let me know if you need some further help Happy Coding :)

                          C 1 Reply Last reply
                          0
                          • C Cracked Down

                            Ok got it! Do following things 1. Build two separate hierarchies for exception 1.1 For exception that you want to catch log e.g. LoggableException 1.2 For exception that you want to you caller to handle e.g. NonLoggableExcptions 2. Only catch the LoggableException exceptions - log in your case 3. Don not catch NonLoggableExcptions - these will be automatically traverse to the caller 4. The try block code should only throw these two exceptions or the derived objects from these exceptions 5. Don not catch Exception, its a bad practice, which says anything could be happen in the code. can it be :-D . this gives bad impression. though I am not sure if any unhanded exception occurs how it is handled in the client server application as I never worked on it hope this helps to you and solve you problem... let me know if you need some further help Happy Coding :)

                            C Offline
                            C Offline
                            Cracked Down
                            wrote on last edited by
                            #13

                            did this solution helped you?

                            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