catch (Exception ex) not displaying MessageBox message for event

82 views Asked by At

So I'm coding a write to function and need the Exception ex to display a warning message that the txtFileContents.Text area is blank and needs to contain some text before the write to function can occur.

Here is my code...

private void btnWriteFile_Click(object sender, EventArgs e)
{
 //The saveFileDialog1 function allows the "Write" function to be used to save text to a file or create a new file when saved.
 if (saveFileDialog1.ShowDialog() == DialogResult.OK)
     //The "try"-"catch" block is used to allow the code to open a directory or file source for the user to browse.
     try
     {
         txtFilePath.Text = saveFileDialog1.FileName;
         StreamWriter sw = new StreamWriter(txtFilePath.Text);
         sw.WriteLine(txtFileContents.Text);
         sw.Close();
     }
     //The below code in the "catch" end of the code block will prompt the user to enter some text if nothing is entered.
     catch (Exception ex)
     {
         Console.WriteLine("Exception: " + ex.Message);
     }
}

Do I need to add a specific MessageBox.Show command line and how would I do that?

I have tried re-wording the Console.WrieLine("Exception: " + ex.Message);

To include the error message inplace of the "exception" wording and nothing happens. I have also tried adding a separate MessageBox.Show line and that didn't execute an error either. I'm new to coding and am trying my hardest to get this right.

0

There are 0 answers