Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8296287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:56:07+00:00 2026-06-08T14:56:07+00:00

When I use a Try/Catch for open a file, the program display a inbuilt

  • 0

When I use a Try/Catch for open a file, the program display a inbuilt message instead of the one I have inside the Catch part, when I try to open a file that don’t exist. Whats wrong and what have I missed?

 public void ReadFromFile(MainFrame obj, string filePath)
    {
        try
        {
            filestream = new FileStream(filePath, FileMode.Open);
            BinaryFormatter b = new BinaryFormatter();
            var animals2 = (List<Animal>)b.Deserialize(filestream);

            foreach (Animal animal in animals2)
            {
                AddAnimalToList(animal);
                obj.UppdateListOfRegistredAnimals(animal.ID, animal.Name, animal.Age, animal.Gender);

            }
            obj.UpdateId(animals.Count());
        }
        catch
        {
            MessageBox.Show("Test", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        finally
        {
            filestream.Close();
        }
    }

EDIT: I discovered that the reason for the inbuilt message is somewhere before the code above! Something must be wrong in the code below that handles an event from the openFileDialog, because despite several messageboxes, non is displayed! What have I done wrong!? Help is preciated!

private void menuFileOpen_Click(object sender, EventArgs e)
    {
        DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
        string file = openFileDialog1.FileName;

        if (result == DialogResult.OK)
        {
            MessageBox.Show("TEST", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            try
            {
                MessageBox.Show("TEST", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                motelManager.ReadFromFile(this, file); // Smart lösning!!

            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("Error message", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }

File not found message

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:56:08+00:00Added an answer on June 8, 2026 at 2:56 pm

    try this

    if(!File.Exists(filePath))
       MessageBox.Show("Not found", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    

    for more info
    http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx

    private void menuFileOpen_Click(object sender, EventArgs e)
        {
            openFileDialog1.CheckFileExists = false;
            openFileDialog1.CheckPathExists = false;
    
            DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
    
    
            if (result == DialogResult.OK)
            {
                string file = openFileDialog1.FileName;
    
    
                MessageBox.Show("TEST", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                try
                {
                    MessageBox.Show("TEST", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    motelManager.ReadFromFile(this, file); // Smart lösning!!
    
                }
                catch (FileNotFoundException)
                {
                    MessageBox.Show("Error message", "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }
    

    if you dont want the default message for file not found use

            openFileDialog1.CheckFileExists = false;
            openFileDialog1.CheckPathExists = false;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

so in my program I have parts where I use try catch blocks like
This article on MSDN states that you can use as many try catch blocks
Possible Duplicate: How to catch exceptions I have not really had to use try
I have a field that may be blank, so I need to use Object.try,
I have a PHP program that encrypts a PDF file into .xxx file this
I have an excel file that i need to open and search it. This
I have a foreach loop that starts a process within a try/catch. In the
Is there some method where one could use a try statement to catch an
I have written a Java program that should open another program (which is a
Is it possible to use TRY CATCH blocks in SQL Selects? For stuff similar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.