Click here to Skip to main content
15,891,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO
    namespace JUSTFORPRACTICE
     {
       public class a
        {
         public static void Main()
         {
            try
            {
                StreamReader str = new StreamReader(@"C:\Users\IBM_ADMIN\Downloads\IBM\metal.txt");
                Console.WriteLine(str.ReadToEnd());//output of str.ReadToEnd() is a string
                str.Close();
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("\n\n");
                Console.WriteLine(e.StackTrace);//It gave information
            }
         }
       }
     }

Code is working fine but my doubt is in catch() we declared a reference variable e for exception class but 'e' is not an instance of the class exception since lacks complete definition as

Exception e = new exception();

So how can we access the Exception class properties as e.Message or e.StackTrace
This silly doubt eating my brain.
Posted
Updated 23-Jun-15 22:59pm
v3
Comments
F-ES Sitecore 24-Jun-15 4:48am    
I don't see any problem with the code, you might to explain a bit further
Annihilated 24-Jun-15 4:55am    
code is working very fine....but my doubt is in the catch() where just Exception e is written over there......does it mean e is an object??
F-ES Sitecore 24-Jun-15 4:58am    
Yes, e is an object of type Exception.
Annihilated 24-Jun-15 5:01am    
but we didnt give full definition as
exception e = new exception() but how by only writing exception e creates object to class exception?
F-ES Sitecore 24-Jun-15 5:19am    
.net passes the exception to your code block in the "e" variable. A function is defined like this

void MyFunc(string blah)
{
Debug.WriteLine(blah.ToUpper());
}

when I call it;

string a = "Hello";
MyFunc(a);

then inside MyFunc the blah variable is "Hello" which is basically the contents of the "a" variable I called it with. So when you define a catch

catch(Exception e)
{
}

then .net "calls" your catch and passes in the exception which becomes the "e" param. You could also do this if you wanted;

catch(Exception xxyyz)
{
Debug.WriteLine (xxyyz.Message);
}

it doesn't have to be "e", it can be anything like a normal function param

1 solution

When you set up a catch block:
C#
try
    {
    ...
    }
catch(Exception e)
    {
    Console.WriteLine(e.Message);
    Console.WriteLine("\n\n");
    Console.WriteLine(e.StackTrace);//It gave information
    }
Or indeed, declare any variable:
C#
string s;
Button b;
StreamReader str;
You aren't creating an instance of the class, you are declaring a place to store a reference to an instance of the class - a container if you like.

Just as a cup can contain coffee, the cup itself is not the wonderful liquid - indeed, when you finish drinking the Coffee instance, you can reuse the same cup to contain a new instance of Coffee, and start drinking that.

As you have noticed, in order to use a class, you need (at some point) to create a new instance of it, and load that into a variable of some form - but you don't always want to create a new one every time. If you write a method that returns a Streamreader:
C#
private StreamReader GetStream(path)
   {
   return new StreamReader(path);
   }

You can create a variable and fill it with an instance without explicitly using the new keyword:
C#
StreamReader str = GetStream(@"C:\Users\IBM_ADMIN\Downloads\IBM\metal.txt");

And that is what effectively happens when you write a catch block: the Exception has already created the new Exception class instance, and you are declarign a variable to put it into. Using new here would imply that it was created at that point, and was unrelated to the error which created and threw the actual exception you are trying to recover information for.
 
Share this answer
 
Comments
Annihilated 24-Jun-15 5:19am    
Thankyou @OriginalGriff
OriginalGriff 24-Jun-15 5:33am    
You're welcome!

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