An Interview Session of Exception Handling: Part 1

An exception is a problem that arises during the execution of a program. We have four keywords: try, catch, finally and throw to handle them.

  • try
  • catch
  • finally
  • throw


Problem 1: Write normal try/catch blocks.

Solution

  1. try  
  2. {  
  3.    // Write Your Code  
  4. }  
  5. catch (Exception ex1)  
  6. {  
  7.    // Handle Your Exception Here  
  8. }  
  9. finally  
  10. {  
  11.    // Finally will execute whether exception occurs or not in code  
  12.    // Write your business requirement   
  13. }  
Problem 2: If I am throwing a DivideByZeroException from a try block and I have 2 catch blocks then the first one with the Exception class and the second with the DivideByZeroException exception class, which will execute?

Solution

It will give an error as in the following:

cs code
                                                                                 Image 1.

You can write your code as in the following:
  1. try  
  2. {  
  3.    throw new DivideByZeroException();  
  4. }  
  5. catch (DivideByZeroException ex1)  
  6. {  
  7.    Console.WriteLine("In DivideByZeroException Catch Block");  
  8. }  
  9. catch (Exception ex)  
  10. {  
  11.    Console.WriteLine("In Exception Catch Block");  
  12. }  
  13. finally  
  14. {  
  15.    Console.WriteLine("Finally Block");  
  16. }  
Output will be:

Output
                                                            Image 2.

Problem 3: Can we have a try without a catch?

Solution: Yes but in this case I need to use a finally block as in the following:

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

c sharp code
                                                                        Image 3.

If I use finally then I can use a try without a catch like in the following code:
  1. try  
  2. {   
  3.    // Write Your Code  
  4.    Console.WriteLine("In Try Block");  
  5. }   
  6. finally  
  7. {  
  8.    // Finally will execute whether exception occurs or not in code  
  9.    // Write your business requirement   
  10.    Console.WriteLine("In Finally Block");  
  11. }  
Output:

program output
                                                                  Image 4.

Problem 4: If I write the following code then will the following exception occur:
  1. try  
  2. {  
  3.    Console.WriteLine("In Try Block");  
  4.    throw new DivideByZeroException();  
  5. }  
  6. catch (DivideByZeroException ex1)  
  7. {  
  8.    Console.WriteLine("In DivideByZeroException Catch Block");  
  9.    throw new DivideByZeroException();  
  10. }  
  11. catch (Exception ex)  
  12. {  
  13.    Console.WriteLine("In Exception Catch Block");  
  14.    throw new DivideByZeroException();  
  15. }  
  16. finally  
  17. {  
  18.    Console.WriteLine("Finally Block");  
  19. }  
Solution: Yes like in the following image.

image
                                                                        Image 5.

Problem 5: What is the difference between Throw Exception and the Throw Clause.

Solution: See both in the following code:

throw code
                                                                              Image 6.

The 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 be overriden and you will lose the original exception stack trace. In other words "throw ex" resets the stack trace.

 


Similar Articles