Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if an exception occurs in Catch block in C#. Also what would be the caller result in that case

It was an interview question, quite simple, but I am not confident about the answer.

What happens if an exception occurs in catch block ?

I am trying to give an example small prog of what the interviewer was trying to ask me, please correct my program if it is not compiling, I am really new to this. Bottom line is what happens if an exception occurs in Catch and what will be the value of caller int hat case.

For instance, I have the following:

double Calculate(int x) {     try     {         x = x/2;     }     catch(Exception ex)     {         Console.Writeline("Message: "+ ex.Message);     }     finally     {       x = 10;     }     return x; }  double myResult = Calculate(x); //x can be any number or 0 for example 

Now there are two questions:

  1. What happens if an exception happens in catch block ? Also, how to resolve it ? (This is simple example of what the interviewer was asking a similar question).

  2. What will happen to myResult if an exception happens in Calculate(x) method ?What will be its value in all cases ? (Please explain every case with an example)

I would like to understand this with a detailed explanation too.

Thank you so much.

like image 563
Jasmine Avatar asked Oct 10 '13 05:10

Jasmine


People also ask

What happens if there is an exception in the catch block?

Answer: When an exception is thrown in the catch block, then the program will stop the execution. In case the program has to continue, then there has to be a separate try-catch block to handle the exception raised in the catch block.

Can you throw an exception in a catch block?

A throw statement can be used in a catch block to re-throw the exception that is caught by the catch statement. The following example extracts source information from an IOException exception, and then throws the exception to the parent method. You can catch one exception and throw a different exception.

Can exception be resolved in catch block?

The purpose of a try-catch block is to catch and handle an exception generated by working code. Some exceptions can be handled in a catch block and the problem solved without the exception being rethrown; however, more often the only thing that you can do is make sure that the appropriate exception is thrown.

How do you handle exceptions inside a catch block?

Place any code statements that might raise or throw an exception in a try block, and place statements used to handle the exception or exceptions in one or more catch blocks below the try block. Each catch block includes the exception type and can contain additional statements needed to handle that exception type.


2 Answers

An exception thrown in a catch block will behave the same as an exception thrown without it - it will go up the stack until it is caught in a higher level catch block, if one exists. Doing this is quite normal if you want to change or wrap the original exception; i.e.:

public void MyStartMethod {     try     {         //do something         MyBadMethod();     }     catch(MySpecialException mse)     {         //this is the higher level catch block, specifically catching MySpecialException      } }  public void MyBadMethod() {     try     {         //do something silly that causes an exception     }     catch (Exception e)     {         //do some logging          throw new MySpecialException(e);     } }  public class MySpecialException : Exception  {        public MySpecialException(Exception e) { ...etc... } } 

In your case, myResult will have whatever value it had before, if it's even still in scope.

like image 162
TheEvilPenguin Avatar answered Sep 20 '22 16:09

TheEvilPenguin


The info below will help (from a previous answer of mine to a related question). If your catch block throws an exception and there are no other catch blocks to handle it besides the one that caused it, it will continue to get re thrown then 'Windows handles it'.

If a exception occurs the CLR traverses up the call stack looking for a matching catch expression. If the CLR doen't finds a matching one, or the Exception gets re thrown each time, the Exception bubbles out of the Main() method. In that case Windows handles the Exception.

Event Handling of Console Applications is the easiest to understand, because there is no special Handling by the CLR. The Exception is leaving the Applications Thread if not caught. The CLR opens a window asking for debug or exit the application. If the user chooses to debug, the debugger starts. If the user chooses to close, the Application exits and the Exception is serialized and written to the console.

like image 32
JuStDaN Avatar answered Sep 22 '22 16:09

JuStDaN