When catching an exception in C# you need not declare a variable for the exception instance if you do not need it. This will avoid the “unused variable” warning.
try { /* ... */ }
catch(Exception) // catches without assigning to a variable
{
// ...
}
Here is how to rethrow an exception without affecting the stack trace:
try { /* ... */ }
catch(System.Data.SqlClient.SqlException ex)
{
if(ex.Number==2627) { /* special case for primary key violation */ }
else throw; // rethrows without affecting the stack trace
// else throw ex; // rethrows with the stack trace reset to this line
}