Exception

1.What are the two diff types of error?
Run time error and Compile time error

2.What is the base class from which all the exceptions are derived ?
System.Exception class

3.What are the two main kind of exceptions class?
System Exception-- Mainly used for inbuilt exceptions like Null Ref Exception,Overflow Exception,Argument ....etc
Application Exception-This class is used for user defined exceptions.

4.How to you handle error in C#?
By using try ,catch and finally blocks.
try-contains the code which might throw an exception
catch-deals with various error conditions
finally-does the cleaning up.

5.Does finally get executed if the code throws an error ?
Finally is always executed.

6.Is it mandatory for a piece of code to have catch or finally when try block is there ?
Yes. If a try block is there then either catch or finally has to be there or else a compiler error is generated.

7.Can finally bock have return statement ?
No,a compile time error is generated. Thats bc finally contains the clean up code.

8.Can there be many catch statements for a single try block ?
Yes.But the rule is most specific exceptions should be caught first.

9.Will the foll code compile ?
           try
            {
                throw new SystemException();
            }           
            catch(Exception e)
            {
            }
            catch(ArgumentException e)
            {
            }
No.It gives the foll error
A previous catch clause already catches all exceptions of this or of a super type ('System.Exception')

10.Whats the syntax for catching all kinds of exception.
try{}
catch         //no parameters specified
{
}
it will catch exceptions thrown by managed as well as unmanaged code.


11.All the foll catch block uses the same exception parameter 'e'. Is it valid?
catch(Exception e) {}
catch(ArgumentException e) {}
Yes,bc the parameter passed to catch block wil be within its scope only.


12.What are diff exception properties ?
a)Helplink-- provide link to help files which give more info on the exception raised.

b)Message--gives the text that describes the exception.
Suppose catch(Exception e)
                {
                       throw new Exception("Failure")
                }
then e.Message will give output as "Failure".

c)Source--provides the name of assembly from where exception is raised.
d)Target Site--name of method thats trows an error
e)Stack Trace--provides stack flow of the exception.
f)Inner Exception--When exception is thrown from one catch to another the message from first catch is passed as inner exception to second catch.

13.What is checked and unchecked operator.
checked--checks  for the error and throws an exception.
unchecked--does not check for the error .truncates the data and prints it..used mainly while casting.

14.Does foll code work ?
try {}
catch(Exception e) {}
catch(Exception e) {}

No,two catch block cannot have same exception classes.

 15.Whats the diff bt throw and throw e ?
throw-- throws the exception.
               throw is internally compiled to rethrow 
throw e--throws the exception and resets the stack trace to current catch
                  location.

                  internally compiled to throw which taken an exception parameter as
                  input.

16.What is the output of foll programs
   a)       try
            {
                throw new SystemException("This is wrong");
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);            
                return;
            }
            finally
            {
                Console.WriteLine("i am inside finally");
                Console.Read();
            }

output--   This is wrong
                     i am inside finally.

b)        try
            {
                throw new SystemException("This is wrong");
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                Console.WriteLine("i am inside finally");
                Console.Read();
            }

output  - This is wrong.
                   Application crashes bc the throw is not being caught anywhere and
                  hence finally wont be executed.

c)public class A
  
    {
        public void methodA()
        {
            try
            {
                B test = new B();
                test.methodB();
            }
            catch (Exception e)           
            {
                throw e;
                Console.WriteLine("Exception from Class A");
            }
            finally          
            {
                Console.WriteLine("Inside Class A Finally");
            }
        }
    }

    public class B
    {
        public void methodB()
        {
            try
            {
                throw new Exception("Exception from Class B");
            }
            catch (Exception e)
            {
                throw;
                Console.WriteLine("Exception from Class A");
            }
            finally
            {
                Console.WriteLine("Inside Class B Finally");
            }
        }
    }

output--inside class B finally.


Not able to see the line number in exception stack trace in remote machine

While building a project make sure .pdb files are generated. copy these files in the folder where you have installed your application in remote machine .
If pdb files are not generated check if the setting is enabled for your project-

Solution->Properties->Build->Advanced Build Settings->Debug Info->pdb-only























           




10 comments:

  1. really very helpful...Good collection of questions... Keep posting more .. :)

    ReplyDelete
  2. The Constitution only gives people the right to pursue happiness. You have to catch it yourself. See the link below for more info.

    #catched
    www.ufgop.org

    ReplyDelete
  3. 16 question
    C option
    the code will throw unhandled code excpetion

    ReplyDelete
  4. Question no.16 c) output is wrong because of throw keyword in Catch block of class B will : Application crashes bc the throw is not being caught anywhere. compiled and executed - so final output would be :An unhandled exception of type 'System.Exception' occurred in ImplementationOopsConcepts.exe

    ReplyDelete
  5. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts . As a beginner in Dot Net programming your post help me ato my career also.Thanks for your informative blog.
    dot net training in velachery | top10 dot net training institutes in chennai

    ReplyDelete
  6. given so much information in it .thanks its veryuseful for my interview preparation.
    dot net training and placement in chennai | best dot net training in chennai

    ReplyDelete
  7. Great, The type of article you describe is exactly what I was looking for .Turkish visa Australia specifically for Australian citizens. With this visa an Australian visa applicant can easily visit in Turkish .



    ReplyDelete