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. C# Exception handling 101

C# Exception handling 101

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharppythonregexquestion
8 Posts 5 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 Offline
    J Offline
    Julien Villers
    wrote on last edited by
    #1

    Here's a 'nice' pattern I saw in some code I've inherited...

    catch (Exception ex)
    {
    if (ex is ExceptionSpecialType)
    throw new SomeException()
    {
    CustomData = (ex as ExceptionSpecialType).SomeThing.ToXmlString()
    };
    else
    {
    // <-- Some specific handling removed here -->
    throw new SomeException()
    {
    CustomData = new CustomError(ex).ToXmlString()
    };
    }
    }

    Oh, and yes, my predecessor loved using 'if (X is T) (X as T).M();'.

    'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail

    A B Sander RosselS J 4 Replies Last reply
    0
    • J Julien Villers

      Here's a 'nice' pattern I saw in some code I've inherited...

      catch (Exception ex)
      {
      if (ex is ExceptionSpecialType)
      throw new SomeException()
      {
      CustomData = (ex as ExceptionSpecialType).SomeThing.ToXmlString()
      };
      else
      {
      // <-- Some specific handling removed here -->
      throw new SomeException()
      {
      CustomData = new CustomError(ex).ToXmlString()
      };
      }
      }

      Oh, and yes, my predecessor loved using 'if (X is T) (X as T).M();'.

      'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail

      A Offline
      A Offline
      Andrei Straut
      wrote on last edited by
      #2

      And I thought my custom unrecoverable Exception handling wrapper sucked:

      Helper.logUnrecoverable(this.GetType().ToString(), "doSync - createHeader or createLine fail. Action rollbacked", e.Message, true);

      Where true is whether it should log the StackTrace or not. But this is a whole different :wtf:-ness level

      Full-fledged Java/.NET lover, full-fledged PHP hater. Full-fledged Google/Microsoft lover, full-fledged Apple hater. Full-fledged Skype lover, full-fledged YM hater.

      1 Reply Last reply
      0
      • J Julien Villers

        Here's a 'nice' pattern I saw in some code I've inherited...

        catch (Exception ex)
        {
        if (ex is ExceptionSpecialType)
        throw new SomeException()
        {
        CustomData = (ex as ExceptionSpecialType).SomeThing.ToXmlString()
        };
        else
        {
        // <-- Some specific handling removed here -->
        throw new SomeException()
        {
        CustomData = new CustomError(ex).ToXmlString()
        };
        }
        }

        Oh, and yes, my predecessor loved using 'if (X is T) (X as T).M();'.

        'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail

        B Offline
        B Offline
        BillW33
        wrote on last edited by
        #3

        Obviously, your predecessor did not have the slightest understanding of proper exception handling. Even reading one article on Code Project about exception would have helped him greatly :-D

        Just because the code works, it doesn't mean that it is good code.

        1 Reply Last reply
        0
        • J Julien Villers

          Here's a 'nice' pattern I saw in some code I've inherited...

          catch (Exception ex)
          {
          if (ex is ExceptionSpecialType)
          throw new SomeException()
          {
          CustomData = (ex as ExceptionSpecialType).SomeThing.ToXmlString()
          };
          else
          {
          // <-- Some specific handling removed here -->
          throw new SomeException()
          {
          CustomData = new CustomError(ex).ToXmlString()
          };
          }
          }

          Oh, and yes, my predecessor loved using 'if (X is T) (X as T).M();'.

          'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail

          Sander RosselS Offline
          Sander RosselS Offline
          Sander Rossel
          wrote on last edited by
          #4

          I've worked with a codebase where each and every method has a try catch block. The catch logged the error and that's that. As a result, every method you called would return normally like everything went fine. Something like an Exception or unexpected results when updating a database record could very well be the result of a ReferenceNull- or InvalidCastException somewhere in some method. Good luck trying to find it :)

          It's an OO world.

          public class Naerling : Lazy<Person>{
          public void DoWork(){ throw new NotImplementedException(); }
          }

          J 1 Reply Last reply
          0
          • Sander RosselS Sander Rossel

            I've worked with a codebase where each and every method has a try catch block. The catch logged the error and that's that. As a result, every method you called would return normally like everything went fine. Something like an Exception or unexpected results when updating a database record could very well be the result of a ReferenceNull- or InvalidCastException somewhere in some method. Good luck trying to find it :)

            It's an OO world.

            public class Naerling : Lazy<Person>{
            public void DoWork(){ throw new NotImplementedException(); }
            }

            J Offline
            J Offline
            Julien Villers
            wrote on last edited by
            #5

            "Look boss, never any error!" In Visual Studio, you can set a kind of breakpoints on exceptions just when they're thrown, I guess it could help with that kind of monstrous codebase.

            'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail

            Sander RosselS 1 Reply Last reply
            0
            • J Julien Villers

              "Look boss, never any error!" In Visual Studio, you can set a kind of breakpoints on exceptions just when they're thrown, I guess it could help with that kind of monstrous codebase.

              'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail

              Sander RosselS Offline
              Sander RosselS Offline
              Sander Rossel
              wrote on last edited by
              #6

              Julien Villers wrote:

              it could help with that kind of monstrous codebase

              We would sit and cry in the corner waiting for help... Help never came ;p

              It's an OO world.

              public class Naerling : Lazy<Person>{
              public void DoWork(){ throw new NotImplementedException(); }
              }

              1 Reply Last reply
              0
              • J Julien Villers

                Here's a 'nice' pattern I saw in some code I've inherited...

                catch (Exception ex)
                {
                if (ex is ExceptionSpecialType)
                throw new SomeException()
                {
                CustomData = (ex as ExceptionSpecialType).SomeThing.ToXmlString()
                };
                else
                {
                // <-- Some specific handling removed here -->
                throw new SomeException()
                {
                CustomData = new CustomError(ex).ToXmlString()
                };
                }
                }

                Oh, and yes, my predecessor loved using 'if (X is T) (X as T).M();'.

                'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail

                J Offline
                J Offline
                Jonathan C Dickinson
                wrote on last edited by
                #7

                WPF by any chance? I was forced to write code like that for it.

                He who asks a question is a fool for five minutes. He who does not ask a question remains a fool forever. [Chineese Proverb] Jonathan C Dickinson (C# Software Engineer)

                J 1 Reply Last reply
                0
                • J Jonathan C Dickinson

                  WPF by any chance? I was forced to write code like that for it.

                  He who asks a question is a fool for five minutes. He who does not ask a question remains a fool forever. [Chineese Proverb] Jonathan C Dickinson (C# Software Engineer)

                  J Offline
                  J Offline
                  Julien Villers
                  wrote on last edited by
                  #8

                  Nope. I'm mostly working in Silverlight, but this is backend code for a webservice, so ASP.NET. I don't see any reason to 'force' you to write bad exception handling code, ie catching Exception, then testing type.

                  'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail

                  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