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.

commented: Nice answer for a "beginner" :) +8

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

commented: +++ +1

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!"

commented: +++ +1

If you got the answer on your question, can you please mark the thread as answered please?
thx
Mitja

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 ....?

or - how can I use exception messages ?

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

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.