C#.NET Exception Handling and Runtime Type Information Interview

6/17/2009 No Comment

C#.NET Exception Handling Interview Questions

C# Error Handling Interview Questions expected in a job interview.

Can structured exceptions be used in C#.NET?
Answer : Yes, Structured Exception Handling is the recommended error-handling mechanism in C#.NET as in most other .NET languages. Most of the .NET framework classes and programs use structured exceptions for errors.

What is the main significance of the System.Exception class in C#.NET?
Answer :  As far as Exception Handling is concerned, the main feature which stands out is the StackTrace property. This property provides a call stack which records where the exception was thrown from. For instance, in the code cited below.
using System;
class CSharpApp

{
public static void Main(){
try
{
fCheck();
}
catch( Exception e )
{
Console.WriteLine( "The System.Exception stack trace = \n{0}", e.StackTrace );
}
}
static void fCheck(){
throw new Exception( "fCheck went out of the way" );
}
}
The output of the above program is as below.

System.Exception stack trace =
  • at CSharpApp.fCheck()
  • at CSharpApp.Main()
This particular stack trace was produced from a debug build and hence a release build may optimize away some of the method calls. In that case it would mean that the call stack would not be what you would be expecting

Can you define your own exceptions in C#?
Answer : Yes, we can by just deriving the exception class from Application.Exception.


Can you describe under what circumstances should you throw an exception?
Answer : One of the most debated topic, this has captured the imagination of C# programmers. Mostly, it is accepted by most in the programming community that exceptions should be thrown only at the time when an 'unexpected' error occurs. Now, the big question arises as to how to decide if an error is expected or unexpected. We can term Exception Handling as a judgement call, but a sheer example of an expected error is failing to read from a file because the seek pointer is at the end of the file, whereas an instance of an unexpected error is failing to allocate memory from the heap during runtime.

Can you tell us if C# have a 'throws' clause as we see in other programming languages?
Answer : No, the "Throws" clause is absent in C#. Unlike Java, C# does not require the programmer/developer to specify the exceptions that a particular method can throw. 

C# Run-time Type Information Interview Questions

Can you tell us how you can check the type of an object at runtime?
Answer : For checking the type, you can  use the "is" keyword. For instance in the example below.

using System;class CSharpApp{
public static void Main(){
string s = "stud";
long i = 10;
Console.WriteLine( "{0} is {1}an integer", s, (IsInteger(s) ? "" : "not ") );
Console.WriteLine( "{0} is {1}an integer", i, (IsInteger(i) ? "" : "not ") );
}
static bool IsInteger( object obj ){
if( obj is int obj is long )
return true;else return false;
}
}
The output of the above program is as below.
  • stud is not an integer
  • 10 is an integer
Is it possible to get the name of a type at program runtime?
Answer : Yes, we can get it by using the GetType method of the object class. For instance in the program below:

using System;
class CSharpTest

{
class CApp{
public static void Main(){
long i = 10;
CSharpTest CSharpTest= new CSharpTest();
DisplayTypeInfo( CSharpTest);
DisplayTypeInfo( i );
}
static void DisplayTypeInfo( object obj ){
Console.WriteLine( "Type name = {0}, full type name = {1}", obj.GetType(), obj.GetType().FullName );
}
}
}
The output of the above program is as below.
  • Type name = Int64 and full type name = System.Int64
  • Type name = CSharpTest and full type name = CSharpTest

No comments :

 

Aired | The content is copyrighted and may not be reproduced on other websites. | Copyright © 2009-2016 | All Rights Reserved 2016

Contact Us | About Us | Privacy Policy and Disclaimer