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. What should you do if assigning is going to crash your application?

What should you do if assigning is going to crash your application?

Scheduled Pinned Locked Moved The Weird and The Wonderful
jsonhelpquestion
17 Posts 9 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.
  • L Lost User

    And before that:

    if (this == null)
    {
    throw new NullReferenceException();
    }

    And from the clouds a mighty voice spoke:
    "Smile and be happy, for it could come worse!"

    And I smiled and was happy
    And it came worse.

    D Offline
    D Offline
    dawmail333
    wrote on last edited by
    #8

    Don't forget this fella:

    Microsoft.Win32.SystemEvents.SessionEnding += (a,b) =>
    {
    MessageBox.Show("An expected error occurred.");
    }

    Don't forget to rate my post if it helped! ;) "He has no enemies, but is intensely disliked by his friends." "His mother should have thrown him away, and kept the stork." "There's nothing wrong with you that reincarnation won't cure." "He loves nature, in spite of what it did to him."

    L 1 Reply Last reply
    0
    • V Vladimir Svyatski

      Another coding horror. Have you ever thought that usual assigning operation, like a=b, can crash your application? Not yet? You gotta be ready. One of my teammates has already prepared:

      public class InspectionMethod
      {
          private string name\_;
      
          \[XmlElementAttribute()\]
          public string Name
          {
              get
              {
                  return name\_;
              }
              set
              {
                  name\_ = value;
              }
          }
      
          //This method is the most interesting part
          public void Resuscitate()
          {
              try
              {
                  name\_ = this.Name;
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message, "Serialization error has happened.",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
              }
          }
      }
      

      And indeed, never forget about message boxes. User must be warned, right?

      Y Offline
      Y Offline
      YvesDaoust
      wrote on last edited by
      #9

      Won't ((InspectionMethod*)BadPointer)->Resuscitate() raise the exception ?

      1 Reply Last reply
      0
      • D dawmail333

        Don't forget this fella:

        Microsoft.Win32.SystemEvents.SessionEnding += (a,b) =>
        {
        MessageBox.Show("An expected error occurred.");
        }

        Don't forget to rate my post if it helped! ;) "He has no enemies, but is intensely disliked by his friends." "His mother should have thrown him away, and kept the stork." "There's nothing wrong with you that reincarnation won't cure." "He loves nature, in spite of what it did to him."

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

        And this, at least if we have multithreaded code:

        catch(ThreadAbortException x)
        {
        throw;
        }

        For one thing,it would lessen the appearance of doing Pokemon exception handling (You must catch them all!) and it may have strange side effects to let this exception simply disappear.

        And from the clouds a mighty voice spoke:
        "Smile and be happy, for it could come worse!"

        And I smiled and was happy
        And it came worse.

        V 1 Reply Last reply
        0
        • V Vladimir Svyatski

          Another coding horror. Have you ever thought that usual assigning operation, like a=b, can crash your application? Not yet? You gotta be ready. One of my teammates has already prepared:

          public class InspectionMethod
          {
              private string name\_;
          
              \[XmlElementAttribute()\]
              public string Name
              {
                  get
                  {
                      return name\_;
                  }
                  set
                  {
                      name\_ = value;
                  }
              }
          
              //This method is the most interesting part
              public void Resuscitate()
              {
                  try
                  {
                      name\_ = this.Name;
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show(ex.Message, "Serialization error has happened.",
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                  }
              }
          }
          

          And indeed, never forget about message boxes. User must be warned, right?

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

          While this is obviously dumb code, in C# and Delphi (and any other language with property support like VB.net), a 'simple assignment' is essentially calling a method so it is as possible to have crash-inducing code in there as it is anywhere else. Particularly if it's something like "myReallyComplicatedRemotingThingy.Connected = true".

          D 1 Reply Last reply
          0
          • L Lost User

            And this, at least if we have multithreaded code:

            catch(ThreadAbortException x)
            {
            throw;
            }

            For one thing,it would lessen the appearance of doing Pokemon exception handling (You must catch them all!) and it may have strange side effects to let this exception simply disappear.

            And from the clouds a mighty voice spoke:
            "Smile and be happy, for it could come worse!"

            And I smiled and was happy
            And it came worse.

            V Offline
            V Offline
            Vladimir Svyatski
            wrote on last edited by
            #12

            It's not only Pokemon exception handling, Smurf naming convention is also widely used in the application I took the snippet from. I just removed it from class name, because it leads in some way to the source of the snippet.

            1 Reply Last reply
            0
            • B BobJanova

              While this is obviously dumb code, in C# and Delphi (and any other language with property support like VB.net), a 'simple assignment' is essentially calling a method so it is as possible to have crash-inducing code in there as it is anywhere else. Particularly if it's something like "myReallyComplicatedRemotingThingy.Connected = true".

              D Offline
              D Offline
              dawmail333
              wrote on last edited by
              #13

              The source code for the property is included. If that causes a crash, you have bigger problems.

              Don't forget to rate my post if it helped! ;) "He has no enemies, but is intensely disliked by his friends." "His mother should have thrown him away, and kept the stork." "There's nothing wrong with you that reincarnation won't cure." "He loves nature, in spite of what it did to him."

              B 1 Reply Last reply
              0
              • D dawmail333

                The source code for the property is included. If that causes a crash, you have bigger problems.

                Don't forget to rate my post if it helped! ;) "He has no enemies, but is intensely disliked by his friends." "His mother should have thrown him away, and kept the stork." "There's nothing wrong with you that reincarnation won't cure." "He loves nature, in spite of what it did to him."

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

                Yes, obviously in the posted example the developer is just an idiot. Just saying, an assignment sign doesn't automatically make a line of code crash-proof. Even a property getter (which looks totally innocuous) can break stuff, though one can argue that that is bad style in itself (Microsoft do it though, for example trying to read Length out of an unbounded stream).

                1 Reply Last reply
                0
                • V Vladimir Svyatski

                  Another coding horror. Have you ever thought that usual assigning operation, like a=b, can crash your application? Not yet? You gotta be ready. One of my teammates has already prepared:

                  public class InspectionMethod
                  {
                      private string name\_;
                  
                      \[XmlElementAttribute()\]
                      public string Name
                      {
                          get
                          {
                              return name\_;
                          }
                          set
                          {
                              name\_ = value;
                          }
                      }
                  
                      //This method is the most interesting part
                      public void Resuscitate()
                      {
                          try
                          {
                              name\_ = this.Name;
                          }
                          catch (Exception ex)
                          {
                              MessageBox.Show(ex.Message, "Serialization error has happened.",
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                          }
                      }
                  }
                  

                  And indeed, never forget about message boxes. User must be warned, right?

                  T Offline
                  T Offline
                  TorstenFrings
                  wrote on last edited by
                  #15

                  too sad you can't override the assignment operator in c#

                  1 Reply Last reply
                  0
                  • V Vladimir Svyatski

                    Another coding horror. Have you ever thought that usual assigning operation, like a=b, can crash your application? Not yet? You gotta be ready. One of my teammates has already prepared:

                    public class InspectionMethod
                    {
                        private string name\_;
                    
                        \[XmlElementAttribute()\]
                        public string Name
                        {
                            get
                            {
                                return name\_;
                            }
                            set
                            {
                                name\_ = value;
                            }
                        }
                    
                        //This method is the most interesting part
                        public void Resuscitate()
                        {
                            try
                            {
                                name\_ = this.Name;
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, "Serialization error has happened.",
                                  MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                    }
                    

                    And indeed, never forget about message boxes. User must be warned, right?

                    K Offline
                    K Offline
                    KP Lee
                    wrote on last edited by
                    #16

                    I love the error message!

                    VUnreal wrote:

                    MessageBox.Show(ex.Message, "Serialization error has happened.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    That's assuming an error is thrown. (I've seen the computer do impossible things several times, so I wouldn't put it past it.) Serialization isn't happening here. No information about where it is happening. Oh shoot, it has ex.Message in it. at first I thought it wouldn't even tell you what was wrong. Oh, lets throw in an icon to show this is an important error message. The name of the routine is interesting. You're in a type safe environment that has an invalid type passed into the string and you are going to "fix" it by passing the invalid type back into the string. Really "advanced" thinking going on here.

                    1 Reply Last reply
                    0
                    • L Lost User

                      And before that:

                      if (this == null)
                      {
                      throw new NullReferenceException();
                      }

                      And from the clouds a mighty voice spoke:
                      "Smile and be happy, for it could come worse!"

                      And I smiled and was happy
                      And it came worse.

                      V Offline
                      V Offline
                      VallarasuS
                      wrote on last edited by
                      #17

                      CDP1802 wrote:

                      if (this == null) { throw new NullReferenceException(); }

                      You don't know how hard we laughed when we found such one in code library.

                      Regards Vallarasu S | BreakingDotNet.blogspot.com

                      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