You are developing an application.
The application contains the following code:
When you compile the code, you receive the following syntax error message: “A previous
catch clause already catches all exceptions of this or a super type (‘System.Exception’).”
You need to ensure that the code can be compiled. What should you do?
A.
Catch the ArgumentException exception instead of the ArgumentNullException exception.
B.
Throw a new exception in the second catch block.
C.
Catch the ArgumentNullException exception first.
D.
Re-throw the exception caught by the second catch block.
The correct answer A is in case if catch(Exception e) is caught first. Probably the question’s source code is badly specified.
The code source can’t be correct, no error would be shown. This would only be the case if the two catch block were in reverse order.
Let’s say it is, answer A would nevertheless not change anything (when exception is caught, argumentexception is caught too, moreover, there isn’t an argumentexception thrown but an argumentnullexception.
Correct answer would be C as I say upper, this would make the code as we have here, which is good.
Dear poster, please do not post wrong question. This kind of question kills the time of the reader’s to find ‘what’s wrong with the code’. absolute time killing activity
Saikat, you’re right. Are you planning to take this exam?
the code is:
try
{
string orderRefNumber = null;
ProccessOrders(orderRefNumber);
}
catch (Exception e)
{
Console.WriteLine(“{0} exception caught”, e);
}
catch (ArgumentNullException e)
{
Console.WriteLine(“{0} exception caught”, e);
}
So answer is: C
catch (ArgumentNullException e){}
{….}
catch (ArgumentException e){}
catch (Exception e){}
A
Marina, thanks.
There is not error with the code. Code is absolutely working fine. So no change is require.
This question is wrong. I tried executing the exact code and does compile successfully. Also just the first exception block is executed.
incorrect question
The right code is:
static void Main()
{
try
{
string orderRefNumber = null;
ProcessOrders(orderRefNumber);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
catch (ArgumentNullException e){
Console.WriteLine(e.Message);
}
}
And the answer is C.