You need to ensure that the code can be compiled

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?

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.



Leave a Reply 11

Your email address will not be published. Required fields are marked *


karfik

karfik

The correct answer A is in case if catch(Exception e) is caught first. Probably the question’s source code is badly specified.

My'nD

My'nD

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.

Saikat

Saikat

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

Shubham Mishra

Shubham Mishra

Saikat, you’re right. Are you planning to take this exam?

Marina

Marina

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

j

j

catch (ArgumentNullException e){}
{….}
catch (ArgumentException e){}
catch (Exception e){}

A

al adeeb

al adeeb

Marina, thanks.

sudhir

sudhir

There is not error with the code. Code is absolutely working fine. So no change is require.

Azmat

Azmat

This question is wrong. I tried executing the exact code and does compile successfully. Also just the first exception block is executed.

msdn

msdn

incorrect question

f9p

f9p

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.