开发者

Catch block without arg

I have a catch block like below:

catch (Exception e)
            {
                connection.Close();
开发者_如何学编程                return null;
            }

I get a warning saying 'e' is not being used. I don't want to use it. But if I remove it I get an error saying "class type expected". What's to be done?


catch
{ 
   connection.Close(); 
   return null; 
} 


do this:

try
{
     //code to throw exception
}
catch(ArgumentNullException)
{
    //do something in response to this exception
}
catch(Exception)
{
    //catch the general exception
}

this allows you to catch different types of exception without having to castch everything as with Danny's answer above (sorry i coulnd't just comment, not enough rating yet!)


Take the e out

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜