0

I've got a bit of code that is being properly triggered, though it seems it's not actually doing what I expect it should be doing.

I'm using System.Text.RegularExpressions.Regex to test a method parameter for proper formatting, and in my tests, the ArgumentException I'm throwing isn't actually halting the application. And there is no try/catch around it either.

When I step through my code, it appears that as soon as it hits the throw new ArgumentException line, it jumps to my Form1_Activated event handler, and then continues on with its business.

void MainForm_Load(object sender, System.EventArgs e)
    {
        SNSBackup.Backup(_saveLocation, _saveLocation + "\\Backups", "*.xml, *.recipex", 5, ArchiveType.Zip);
    }

public static void Backup(string source, string destination, string ext, int backupsToKeep, ArchiveType type)
        {
            // Test to see if the 'ext' parameter is in a valid format.
            // Makes sure that the
            Regex r = new Regex(@"^(\*\.\w+\s*\,?\s*)+$");

            if (!r.IsMatch(ext))
                throw new ArgumentException();
Mirrana
  • 1,601
  • 6
  • 28
  • 66

1 Answers1

1

I believe this is something within WinForms known as a silent exception. Basically, your application will essentially swallow exceptions that are thrown an unhandled (as far as I know). You can alter some settings to prevent this / handle it - see this and this related question.

Note: There seems to be mention in other posts that this is an issue isolated to 64-bit platforms. I'm not sure if this is the case or not.

Community
  • 1
  • 1
Samuel Slade
  • 8,405
  • 6
  • 33
  • 55
  • After looking through those threads, I've decided to try using the ThreadException event. Though I'm confused, because some people seem to imply that doing so id a bad idea, when others say it's the best thing you could do. – Mirrana Jan 12 '12 at 16:11
  • 2
    Yes, it is a 64-bit platform issue, nothing to do with WinForms. Explained best [here](http://stackoverflow.com/a/4934010/366904). The most pertinent suggestion in this case is the one to reconsider placing code in the `Load` event to begin with. It probably doesn't belong there. – Cody Gray - on strike Jan 13 '12 at 00:13