I have a class with constructor, if i throw an error inside the constructor what will become the object while initializing

Showing Answers 1 - 1 of 1 Answers

if you throw an error within the constructor of a class, your code will compile but the object will not be instantiated and will through an error. check this sample code:

using System;

using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

Program objpr = new Program();

Console.WriteLine(objpr);

}

public Program()

{

System.IO.StreamReader objReader = new System.IO.StreamReader("text.txt");

try

{

Console.WriteLine(objReader.ReadToEnd());

}

catch (System.IO.FileNotFoundException exc)

{

new Exception(exc.Message);

}

finally

{

objReader.Close();

}

}

}

}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions