Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I made an application to view the text data of a file using one textbox txtdescription
and button display_message and wrote following code on button event
C#
try
{
TextReader tr = new StreamReader("asd.txt");
try
{ txtdescription.Text = tr.ReadToEnd(); }
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
finally
{ tr.Close(); }
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }


Application is running

C#
catch (Exception ex)
{ MessageBox.Show(ex.Message); 


this catch will occur when i "ll define that file which do not exist

but i am not getting the concept of fllowin try catch try

C#
txtdescription.Text = tr.ReadToEnd(); }
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
finally
{ tr.Close(); }


when will this catch occur???

Thanx ion advance
Posted

So strange code style.. I think, in your case there is no sence for nested try-catch block.

Maybe it's better to use this:

C#
try
{
   TextReader tr = new StreamReader("asd.txt");
   txtdescription.Text = tr.ReadToEnd();
}
catch (ConcreteException ex)
{ 
   MessageBox.Show(ex.Message); 
}
catch (Exception ex)
{ 
   MessageBox.Show(ex.Message); 
}
finally
{ 
   tr.Close(); 
}


But if you really want to get exception in your code, you can write
C#
tr.Close()
before
C#
txtdescription.Text = tr.ReadToEnd();

Or you can read about TextReader ReadToEnd method[^] and think of case, when this exceptions can be raised.
 
Share this answer
 
Comments
E.F. Nijboer 2-Nov-11 8:24am    
Declaration and instantiation of the TextReader would need to be before the try block. The compiler will probably give you a warning otherwise.
Nickos_me 2-Nov-11 8:36am    
Oh,really. You are right, my fault.
shivani 2013 3-Nov-11 8:18am    
Thank you,I got it
There isn't any logic for the extra surrounding try..catch because the only statement is the declaration and instantiation of a StreamReader class. It would be very bad practice to throw an exception from a constructor so that won't happen. (unless memory is that filled up that this new class will give an out-of-memory but that is not something you need to consider). The outer try.catch therefor can be removed safely.

Maybe a better way is to use "using". This will ensure proper destruction of the object when out of scope (stream will be closed) and that way the finally isn't needed.

C#
class Test
{
    public static void Main()
    {
        try
        {
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader("TestFile.txt"))
            {
                string line;
                // Read and display lines from the file until the end of
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }
        }
        catch (Exception e)
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }
    }
}


Good luck!
 
Share this answer
 
Comments
shivani 2013 3-Nov-11 8:18am    
Thank you,I got it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900