Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# value is declared but never used

Tags:

c#

I have a try and catch where I am catching different error types:

catch (XmlException XmlExp)
{
    return false; 
}

catch (XmlSchemaException XmlSchExp)
{
    return false; 
}
catch (Exception GenExp)
{
    throw GenExp;
}

Notice that XMLException XMLExp is declared but never used. Is it possible to catch the XMLException without declaring a local variable?

Is this valid code:

catch (XmlException)
{
    return false; 
}
like image 751
JL. Avatar asked Dec 11 '09 11:12

JL.


1 Answers

Yes, like this

catch (XmlException)
{
    return false; 
}

catch (XmlSchemaException)
{
    return false; 
}

catch (Exception GenExp)
{
     // inspect or use GenExp
     throw;
}
like image 87
Mark Seemann Avatar answered Sep 28 '22 15:09

Mark Seemann