Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

An Interview Session Part 1- Exception Handling (Try Catch and Finally)

Article:
Viewed:  5690 
Posted On:  21/12/2014 09:48:32 
Interview questions asked on exception handling on c# .NET. 

 

An exception is a problem that arises during the execution of a program. C# exception handling is built upon four keywords: try, catch, finally and throw.

  • try: A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.
  • catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.
  • finally: The finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not.


throw: This keyword is used to throw an exception explicitly. This is done using a throw keyword.

Exception Classes in C#

C# exceptions are represented by classes. The exception classes in C# are mainly directly or indirectly derived from the System.Exception class. Some of the exception classes derived from the System.Exception class are the System.ApplicationException and System.SystemExceptionclasses.

The System.ApplicationException class supports exceptions generated by application programs. So the exceptions defined by the programmers should derive from this class.

The System.SystemException class is the base class for all predefined system exception.

 

Problem 1 # Write Normal Try Catch Statement:

Solution #   

            try
            {
                // Write Your Code
            }
            catch (Exception ex1)
            {
                // Handle Your Exception Here
            }
            finally
            {
                // Finally will execute whether exception occurs or not in code
               // Write your business requirement
            }
 

Problem 2 # If I am throwing a DivideByZeroException from try block and I have 2 Catch block first one with Exception class and second with DivideByZeroException  exception class which will execute?

Solution # It will give error like below:

Prob 2_1.png

Image 1.

You can write your code like below:

            try
            {
                throw new DivideByZeroException();
            }
            catch (DivideByZeroException ex1)
            {
                Console.WriteLine("In DivideByZeroException Catch Block");
            }
            catch (Exception ex)
            {
                Console.WriteLine("In Exception Catch Block");
            }
            finally
            {
                Console.WriteLine("Finally Block");
            }

Output will be:

Prob 2_2.png

Image 2.

Problem 3 # can be having try without Catch?

Solution # Yes but in this case I have to use finally block like below

First if I don't use finally block then it will give error:

Prob 3_1.png

Image 3.

If I use finally then I can use try without catch like in below code:

            try
            {               
                // Write Your Code
                Console.WriteLine("In Try Block");
            }          
            finally
            {
                // Finally will execute whether exception occurs or not in code
                // Write your business requirement
                Console.WriteLine("In Finally Block");
            }

Output:

Prob 3_2.png

Image 4.

Problem 4 # If I write below code then will exception will occur:

            try
            {
                Console.WriteLine("In Try Block");
                throw new DivideByZeroException();
            }
            catch (DivideByZeroException ex1)
            {
                Console.WriteLine("In DivideByZeroException Catch Block");
                throw new DivideByZeroException();
            }
            catch (Exception ex)
            {
                Console.WriteLine("In Exception Catch Block");
                throw new DivideByZeroException();
            }
            finally
            {
                Console.WriteLine("Finally Block");
            }

Solution # Yes like in below image.

Prob 4_1.png

Image 5.

Problem 5 # Difference between Throw Exception and Throw Clause

Solution# See both in code:

Prob 5_1.png

Image 6.

 That difference is the Stack Trace Information that gets sent with the exception
"When you throw an exception using "throw ex" then you override the original stack trace with a new stack trace that starts from the throwing method."

Throw

In Throw, the original exception stack trace will be retained. To keep the original stack trace information, the correct syntax is 'throw' without specifying an exception.

Throw ex

In Throw ex, the original stack trace information will get override and you will lose the original exception stack trace. I.e. 'throw ex' resets the stack trace.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us