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:
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).
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…