神刀安全网

C# Interview Questions on Exception Handling

In this article, we will discuss the frequently asked interview questions on Exception Handling in C#

  1. What is an exception?
  2. Explain exception handling mechanism in C#?
  3. Explain the difference between error and exception in C# ?
  4. Explain the hierarchy of exceptions in C#?
  5. What is the main use of a finally block in exception handling?
  6. What is custom exception and how to raise it in C#?
  7. What is the base class from which all the exceptions are derived?
  8. Does finally get executed if the code throws an error?
  9. What is the difference between System exceptions and Application exceptions?
  10. What is the difference between throw and throw ex ?

What is an exception?

  • A .NET exception is an object that describes a runtime error condition.
  • When a runtime error condition arises, an object is created that represents the runtime error and is thrown in the method that has caused the error.
  • That method may handle the exception itself or pass it on. Whatever may be the way, the exception is caught and processed at some point.
  • Exceptions can be generated by the CLR or can be generated by the code.
  • Exceptions thrown by .NET are related with primary errors that violate the rules of the C# language or the constraints of the .NET execution environment
  • .NET exception handling is done with try, catch, throw and finally.
  • A try block contains program statements that are required to be monitored

Explain exception handling mechanism in C#?

Exceptions in C# are handled using try, catch and finally blocks.

try block : The code or set of statements which are to be monitored for exception are kept in this block.

catch block : This block catches the exceptions occurred in the try block.

finally block : This block is always executed whether exception is occurred in the try block or not and occurred exception is caught in the catch block or not.

Explain the difference between error and exception in C# ?

Exception handling is a mechanism to detect and handle error at run time where as Errors are occured at development/compile time.

Explain the hierarchy of exceptions in C#?

C# Interview Questions on Exception Handling

What is the main use of a finally block in exception handling?

The finally block is linked with a try block and contains a block of statements that are executed irrespective of whether an exception occurs

or not within the try block.Finally defines the code that is executed always.

In the normal execution it is executed after the try block When an exception occurs, it is executed after the handler if any or before propagation as the case may be.

What is custom exception and how to raise it in C#?

  1. Custom exceptions can be defined as per requirement
  2. Build the custom exception class by inheriting Exception class
  3. To raise the custom exception at appropriate moment use throw myExcepObj;
class myException : Exception { /*Implement the constructors by just calling the base class constructor*/ public myException() : base() {} public myException( string msg ) : base(msg) {} } 

Example:

usingSystem; class InvalidAgeException : Exception { public InvalidAgeException() : base() {} public InvalidAgeException(string msg) : base(msg) {} } class Person { private int age; public int Age { set { if ( value >= 19 && value <= 60 ) age = value; else{ InvalidAgeException expObj = new InvalidAgeException("The Age input has to be with 19 to 60"); throw expObj; } } get { return age; } } } class Test { public static void Main(string []args) { PersonpersonObj = new Person(); Console.Write("Enter the age : "); try { string ageInput = Console.ReadLine(); /* convert the string value into int value with the help of int.parse() method */ personObj.Age = int.Parse( ageInput ); Console.WriteLine("Valid age input"); } catch(InvalidAgeExceptionexpObj){ Console.WriteLine( expObj.Message ); } } } 

What is the base class from which all the exceptions are derived?

System.Exception

Does finally get executed if the code throws an error?

Yes, Finally block will get executed always.

What is the difference between System exceptions and Application exceptions?

System exceptions are derive directly from a base class System.SystemException. A System level Exception is normally thrown when a nonrecoverable error has occurred.

Application exceptions can be user defined exceptions thrown by the applications. If you are designing an application that needs to create its own exceptions class, you are advised to derive custom exceptions from the System.ApplicationException class. It is typically thrown when a recoverable error has occurred.

What is the difference between throw and throw ex?

throw statement preserves original error stack information but in throw ex, stack error of exception will be replaced with a stack trace starting with rethrow point.

© 2016,admin. All rights reserved.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » C# Interview Questions on Exception Handling

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮