Search code examples
c#wpftext-to-speechspeech-synthesis

C# Speech Synthesis issue


I have run into an issue that has thrown me for a loop since last night. I decided that in order for my program to be as user friendly as possible, I should do some exception handling so a user would know why it isn't working. However, No matter how I tried to catch the System.Argument Exception, it still would be thrown. I'm not looking for someone to just give me some code to fix this, I would really like someone to explain why this is happening so I know what to do to handle this sort of problem in the future.

using System.Speech.Synthesis;

namespace SpeechProject
{
    /// <summary>
    /// Interaction logic for EnglishChinese.xaml
    /// </summary>
    public partial class EnglishChinese : Page
    {
        public EnglishChinese()
        {
            InitializeComponent();
        }

        private void speakBtn_Click(object sender, RoutedEventArgs e)
        {
            SpeechSynthesizer synth = new SpeechSynthesizer();

            try
            {
                synth.SelectVoice("Microsoft Hanhan Desktop");
                synth.Speak(spokenWords.Text);
            }

            catch (ArgumentException)
            {
                MessageBox.Show("You need to install the China Chinese Simplified Language Pack to use this feature");
            }
        }
    }
}

I then tried a different approach from this one. I tried an if else combination. Although, i'm pretty sure i'm not doing something correctly with that said if statement. But it does however work for using "Microsoft Zira Desktop" but does not work for the others.

 public partial class EnglishChinese : Page
    {
        public EnglishChinese()
        {
            InitializeComponent();
        }

        private void speakBtn_Click(object sender, RoutedEventArgs e)
        {
            SpeechSynthesizer synth = new SpeechSynthesizer();

            if (synth.Voice.Name == "Microsoft Huihui Desktop")
            {
                synth.SelectVoice("Microsoft Huihui Desktop");
                synth.Speak(spokenWords.Text);
            }

            else
            {
                MessageBox.Show("You need to install the China Chinese Simplified Language Pack to use this feature");
            }
        }
    }
}

// It automatically throws the message box, when I click the button

This is rather confusing since if it works for one, it should work for all, but that isn't the case with this if statement structure. Any tips would be very much appreciated.

Results with the catch Exception ex and Argument Exception ext: enter image description here


Solution

  • Stewart_R, Psudonym and EBrown all helped answer this question. Debug settings needed to be changed to reflect how I wanted certain exceptions to be handled. And the Try Catch method was the better method to use instead of the If Else statements. I had to make sure I caught the specific exception I wanted, in this case it was ArgumentException. Thank you guys for your help. I really appreciate it.