good evening))
(At us now evening))
Please answer the question -
there's a code fragment -

public string MAP(string FilePath) 
       {
           FileStream fs; // объявляем байтовый поток
           try { fs = new FileStream(FilePath, FileMode.Open); } 
           catch (FileNotFoundException exp) { [B][U]return[/U][/B] exp.Message; }
           catch  { [U][B]return[/B][/U] "not possible to open a file"; }
           return "all right!" ;
           
       }

Question - do the first two returns concern value which is returned by function?
may this function return "not possible to open a file" or exp.Message ?
Thank you for your answers ))

I think it would be better if you throw a new exception in the catch rather than returning a string. Handle the new exception in the caller method.

Comments
Nice answer for a "beginner" :)

If you want to check if something has happened with the code correctly or not, you better use boolean values. This is a simpe example:

private void GoingIntoMAP()
        {
            string path = @"C:\1\myFile.txt";
            bool bSuccess = MAP(path);
            if (bSuccess)
                MessageBox.Show("An error has occured while reading file: " + path);
            else
                MessageBox.Show("File has beed read successfully."); //but this is not necessary to show, only errors or warnings!
        }

        public bool MAP(string FilePath)
        {
            using (FileStream fs = new FileStream(FilePath, FileMode.Open))
            {
                try
                {
                    //read the file and stuff you want to do!
                    //on the end:
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

I hope this explains the concept of catching error.
Do some examples and you will see this is the best way.

Mitja

Comments

To answer your question, rather than tell you how to do it, yes, it could return those values. If the file doesn't exist, it will return whatever is in exp.Message. If some other error occurs it will return "not possible to open a file". Otherwise it returns "all right!"

Comments

I think it would be better if you throw a new exception in the catch rather than returning a string. Handle the new exception in the caller method.

If my function returns no value - how can I display an error on the screen ....?

Edited 5 Years Ago by vedro-compota: n/a

catch(Exception ex)
{
    MessageBox.Show(ex.Message,"Error message");
}

Edited 5 Years Ago by Mitja Bonca: n/a

good evening))
(At us now evening))
Please answer the question -
there's a code fragment -

public string MAP(string FilePath) 
       {
           FileStream fs; // объявляем байтовый поток
           try { fs = new FileStream(FilePath, FileMode.Open); } 
           catch (FileNotFoundException exp) { [B][U]return[/U][/B] exp.Message; }
           catch  { [U][B]return[/B][/U] "not possible to open a file"; }
           return "all right!" ;
           
       }

Question - do the first two returns concern value which is returned by function?
may this function return "not possible to open a file" or exp.Message ?
Thank you for your answers ))

If the try is successful, it will return: "all right!"
Else, it will return the exception message.

Once it returns, it has returned. Lol!

Edited 5 Years Ago by iconoclazt: n/a

This question has already been answered. Start a new discussion instead.