Real time& in-depth interview questions and answers C#

Welcome to real time world. here you would be learning all the Real time C# questions asking in interviews.

Exception handling:

1. Which catch block will be executed in the following code snippet.

Code Snippet 1
  1. using System;
  2. namespace CSharpIQA
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             try
  9.             {
  10.                 throw new DivideByZeroException();
  11.             }
  12.             catch (Exception ex)
  13.             {
  14.                 Console.WriteLine(“Exception”);
  15.             }
  16.             catch (DivideByZeroException dex)
  17.             {
  18.                 Console.WriteLine(“DivideByZeroException”);
  19.             }
  20.             catch (NullReferenceException nex)
  21.             {
  22.                 Console.WriteLine(“NullReferenceException”);
  23.             }
  24.         }
  25.     }
  26. }

Output : In the above code snippet the output will be a compile time error.

Why: Exception is parent type of all exceptions so it should be placed at last in the catch block order, that is why we will get compile time error saying A previous catch clause already catches all exceptions of this or of a super type (‘Exception’)  as shown in the below picture.

that means if you want to use parent Exception at fist place, you don’t need to mention other child exception types.

image

Solution: If you want to use all parent and child exceptions, you can do that by placing parent exception type at last as shown in the below code snippet 2.

Code Snippet 2
  1. using System;
  2. namespace CSharpIQA
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             try
  9.             {
  10.                 throw new DivideByZeroException();
  11.             }
  12.             catch (DivideByZeroException dex)
  13.             {
  14.                 Console.WriteLine(“DivideByZeroException”);
  15.             }
  16.             catch (NullReferenceException nex)
  17.             {
  18.                 Console.WriteLine(“NullReferenceException”);
  19.             }
  20.             catch (Exception ex)
  21.             {
  22.                 Console.WriteLine(“Exception”);
  23.             }
  24.         }
  25.     }
  26. }

2. What is the output of the following code snippet 1

Case 1. when exceptioned

Case 2. when no exception

Code Snippet 1
  1. using System;
  2. namespace CSharpIQA
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             Console.WriteLine(Sum());
  9.         }
  10.         public static int Sum()
  11.         {
  12.             int a = 10, b = 20;
  13.             try
  14.             {
  15.                 return a + b;
  16.             }
  17.             catch (Exception ex)
  18.             {
  19.                 return -1;
  20.             }
  21.             finally
  22.             {
  23.                 return 0;
  24.             }
  25.             return 0;
  26.         }
  27.     }
  28. }

Output: Compile time error in both the cases.

Why?:  we can’t write return statement in finally block.

You can see the actual error thrown by compiler in the below screenshot

image

Solution: just remove the return statement from finally statement.

3. What is the output of the following code snippet 1

Code Snippet 1
  1. using System;
  2. namespace CSharpIQA
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             TestMethod();
  9.         }
  10.         public static int TestMethod()
  11.         {
  12.             try
  13.             {
  14.                 Console.WriteLine(“try”);
  15.                 throw new DivideByZeroException(“”);
  16.                 return 0;
  17.             }
  18.             catch (Exception ex)
  19.             {
  20.                 Console.WriteLine(“catch”);
  21.                 return 0;
  22.             }
  23.             finally
  24.             {
  25.                 Console.WriteLine(“finally”);
  26.             }
  27.             Console.WriteLine(“body”);
  28.             return 0;
  29.         }
  30.     }
  31. }

Output:

1. Normally we think that output is

try

catch

but this output is wrong.

2. The correct output is

try

catch

finally

when we look at return statement in catch block, we think that the cursor will return from there, but finally block will be executed always. That is why the output is try, catch, finally

4. What are exception filters?

coming soon…

5. What is the difference between an error and exception?

An exception is a class that takes advantage of language semantics. As others have stated, exceptions interrupt execution up the stack until caught. An exception can be used to convey an error, but more generally is used to convey that something exceptional has occurred.

Errors, on the other hand, can be exceptional or not.

There are several kinds of errors:

  • User error – this should be handled without an exception
  • Syntax error – this shouldn’t compile in statically typed languages (in dynamic languages, they’re a little harder to discover)
  • Runtime error – this will either result in an exception, or silently fail (usually creating unexpected results)

Really, exceptions should be limited to handling runtime errors, since a user inputting bad data is not “exceptional.” To handle user errors, you should take the following approaches:

  • Prevent bad data from being input (front-end validation)
  • Prevent bad data from being persisted (back-end validation)

Exceptions should be used as a “last line of defense” for user error. If you’re writing a persistence layer, you can rely on exceptions to ensure that bad data that falls through validation does not get persisted. You should, however, fix any of these by putting a fix in the validation that prevents the error from occurring in the first place.

Source: https://stackoverflow.com/questions/2435152/what-is-the-difference-between-an-error-and-an-exception-in-net

preparing more questions