C#.NET Exception Handling Interview Questions
C# Error Handling Interview Questions expected in a job interview.
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;The output of the above program is as below.
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" );
}
}
System.Exception stack trace =
- at CSharpApp.fCheck()
- at CSharpApp.Main()
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.
Answer : Yes, we can get it by using the GetType method of the object class. For instance in the program below:
- stud is not an integer
- 10 is an integer
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 :