Will this code compile?
try
{

}
catch
{
string s="error";
throw s;
}

 Posted by Ddd on 3/7/2011 | Category: C# Interview questions | Views: 3122 | Points: 40
Answer:

No, it won't compile.

For throw, the type must be derived from the Exception class.
throw/catch keywords always expect an Exception instance.
example:
This would be Ok.
    

try
{

}
catch
{
Exception e=new Exception();
throw e;
}


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Kishork80 on: 3/9/2011 | Points: 10
Good example.
Posted by: Sathya4260 on: 3/10/2011 | Points: 10
Can u tell me how to throw the exception with the world "Sorry"
Posted by: Ddd on: 3/12/2011 | Points: 10
Write this command:
throw new Exception("Sorry");

Login to post response