The variable 'exception' is declared but never used
I also see a lot of these warnings
catch(Exception ex)
{
return false;
}
(The variable ‘ex’ is declared but never used)
Please remember that there is no need to declare the exception object if you aren’t going to use it. This same code can simply be written as
catch
{
return false;
}
It saves memory and if no Exception type is specified in the catch block, System.Exception is assumed. Even if you need to throw it, the following code will work fine
catch
{
throw;
}
If you need to catch some specific exception, you can still do it without declaring the object
catch (ApplicationException)
{
throw;
}
Or
catch (ApplicationException)
{
return false;
}
[Update]
While writing
catch
{
throw;
}
is as good as not writing anything, writing
catch(Exception Ex)
{
throw Ex;
}
effectively clears out any existing StackTrace in Ex; probably not a good idea.
catch(Exception ex)
{
return false;
}
(The variable ‘ex’ is declared but never used)
Please remember that there is no need to declare the exception object if you aren’t going to use it. This same code can simply be written as
catch
{
return false;
}
It saves memory and if no Exception type is specified in the catch block, System.Exception is assumed. Even if you need to throw it, the following code will work fine
catch
{
throw;
}
If you need to catch some specific exception, you can still do it without declaring the object
catch (ApplicationException)
{
throw;
}
Or
catch (ApplicationException)
{
return false;
}
[Update]
While writing
catch
{
throw;
}
is as good as not writing anything, writing
catch(Exception Ex)
{
throw Ex;
}
effectively clears out any existing StackTrace in Ex; probably not a good idea.
Comments
Post a Comment